[ad_1]
I’m working with NestJs, Typeorm and Postgresql.
I’m trying to use ManyToOne relation in embedded entity. I need to load foreign key column in node environment, so added one additional column(createdById column below). This makes problem.
Here is my code.
A.ts
@Entity()
export class A {
@PrimaryGeneratedColumn()
id!: number;
@Column(() => Embed, { prefix: false })
embed!: Embed;
@CreateDateColumn({ name: 'created_at' })
createdAt!: Date;
}
Embed.ts
@Entity()
export class Embed {
@Column()
x!: number;
@Column()
y!: number;
@ManyToOne(() => B)
@JoinColumn({ name: 'created_by_id' })
createdBy?: B;
@Column({ name: 'created_by_id' })
createdById!: number;
}
B.ts
@Entity()
export class B {
@PrimaryGeneratedColumn()
id!: number;
@CreateDateColumn({ name: 'created_at' })
createdAt!: Date;
}
When I run the app with option TYPEORM_SYNCHRONIZE=true
and TYPEORM_LOGGING=true
, I get error messages like query failed: CREATE TABLE "a" ("id" SERIAL NOT NULL, "created_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "created_by_id" integer NOT NULL, "created_by_id" integer NOT NULL, "x" integer NOT NULL, "y" integer NOT NULL, CONSTRAINT "PK_684f21444e543375e4c2e6f27fe" PRIMARY KEY ("id"))
, Message: column \"created_by_id\" specified more than once.
. Typeorm trying to create created_by_id
column twice. (I applied custom NamingStrategy so that column of embedded entity’s name to be snake_case)
If I place createdBy
and createdById
column to A
directly, then it makes no error. Is it problem of typeorm version? Or any other solutions?
package version:
"dependencies": {
"@nestjs/typeorm": "7.1.0,
"typeorm": "0.2.31",
}
run with docker container,
node image: 16.14.2-alpine3.15,
postgres image: mdillon/postgis:11-alpine
[ad_2]