Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

StackOverflow Point

StackOverflow Point Navigation

  • Web Stories
  • Badges
  • Tags
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Web Stories
  • Badges
  • Tags
Home/ Questions/Q 1916
Alex Hales
  • 0
Alex HalesTeacher
Asked: May 31, 20222022-05-31T07:28:24+00:00 2022-05-31T07:28:24+00:00

postgresql – typeorm relation in embedded column

  • 0

[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]

  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Related Questions

  • xcode - Can you build dynamic libraries for iOS and ...

    • 0 Answers
  • bash - How to check if a process id (PID) ...

    • 4795 Answers
  • database - Oracle: Changing VARCHAR2 column to CLOB

    • 1063 Answers
  • What's the difference between HEAD, working tree and index, in ...

    • 1009 Answers
  • Amazon EC2 Free tier - how many instances can I ...

    • 0 Answers

Stats

  • Questions : 43k

Subscribe

Login

Forgot Password?

Footer

Follow

© 2022 Stackoverflow Point. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.