Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Predefined table name not being used when using Entity Manager API #10837

Open
SaintPepsi opened this issue Apr 18, 2024 · 1 comment
Open

Predefined table name not being used when using Entity Manager API #10837

SaintPepsi opened this issue Apr 18, 2024 · 1 comment

Comments

@SaintPepsi
Copy link

When using the datasource manager to perform database operations, the table name used is the class name rather than the name specified in the @Entity({ name: 'your_table_name' }) this happens to all my entities

@Entity({ name: GoalEntity.table_name })
export class GoalEntity extends BaseEntity {
    public static table_name = 'goal' as const;

    @PrimaryGeneratedColumn()
    id!: number;

    @Column({ type: 'int', nullable: true })
    user_id!: number;

    @Column({ type: 'boolean' })
    is_sync_deleted: boolean = false;

    @CreateDateColumn()
    created_at!: Date;

    @UpdateDateColumn()
    updated_at!: Date;

    @ManyToOne(() => GoalCategoryEntity, (goal_category) => goal_category.id)
    goal_category!: GoalCategoryEntity;

    @ManyToOne(() => GoalFrequencyEntity, (goal_frequency) => goal_frequency.id)
    goal_frequency!: GoalFrequencyEntity;

    @ManyToOne(() => GoalMethodEntity, (goal_method) => goal_method.id)
    goal_method!: GoalMethodEntity;

    @Column({ type: 'integer' })
    desired_goal_amount!: number;

    @Column({ type: 'integer' })
    current_goal_amount!: number;

    @OneToMany(() => CalendarEntryEntity, (calendar_entry) => calendar_entry.id)
    calendar_entries!: CalendarEntryEntity[];
}

When the migration runs it clearly creates the table as how I specify it:

await queryRunner.createTable(
    new Table({
        name: 'goal',
        columns: [
            {
                name: 'id',
                type: 'integer',
                isPrimary: true,
                isGenerated: true,
                generationStrategy: 'increment'
            },
            { name: 'user_id', type: 'integer', isNullable: true },
            { name: 'is_sync_deleted', type: 'boolean', default: false },
            { name: 'created_at', type: 'timestamp', default: 'now()', isNullable: false },
            { name: 'updated_at', type: 'timestamp', default: 'now()', isNullable: false },
            { name: 'desired_goal_amount', type: 'integer' },
            { name: 'current_goal_amount', type: 'integer' },
            // Foreign Keys
            { name: 'goal_category_id', type: 'integer', isNullable: false },
            { name: 'goal_frequency_id', type: 'integer', isNullable: false },
            { name: 'goal_method_id', type: 'integer', isNullable: false }
        ]
    }),
    true
);

which outputs the following correct query during migration:

[CREATE TABLE "goal"](query:  CREATE TABLE "goal" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "user_id" integer, "is_sync_deleted" boolean NOT NULL DEFAULT (false), "created_at" timestamp NOT NULL DEFAULT (now()), "updated_at" timestamp NOT NULL DEFAULT (now()), "desired_goal_amount" integer NOT NULL, "current_goal_amount" integer NOT NULL, "goal_category_id" integer NOT NULL, "goal_frequency_id" integer NOT NULL, "goal_method_id" integer NOT NULL))`

However when I use the Repository/EntityManager API like so:

dataSource.manager.find(GoalEntity)

The query uses the class name rather than the predefined name:

SELECT "GoalEntity"."user_id" AS "GoalEntity_user_id"... etc

It should be creating something like this:

SELECT "goal"."user_id" AS "goal_user_id"... etc

I will try and create a minimal reproducible example when I have some more time 👍

Regards,
Ian

@SaintPepsi
Copy link
Author

I've created a minimal reproducible example:
https://github.com/SaintPepsi/typeorm-incorrect-table-name

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant