Skip to content

Relationship column names #258

Closed
Closed

Description

@WoLfulus

All our columns has a "id_" prefix for FKs. When dealing with relations, I ended up creating variables with id_ prefix too

Is there any way to set custom colum names for relations to avoid accessing those instances with a "id_" variable?

Activity

  1. pleerock commented on Feb 3, 2017

    @pleerock
    Member

    Yeah you should be able to do so:

    @Table()
    export class Post {
        @PrimaryGeneratedColumn()
        id: number;
    
        @OneToOne(type => Members, member=>member.post)
        @JoinColumn({ name: "member_id" })
        member: Members;
    
       // this is optional:
       // @Column({  type: "int", nullable: true })
       // memberId: number;
    
        @Column()
        comment:string;
    }
  2. WoLfulus commented on Feb 6, 2017

    @WoLfulus
    Author

    Thanks!

  3. petahbred commented on Nov 22, 2018

    @petahbred

    How does one do this in vanilla javascript?

  4. netojose commented on Jul 17, 2020

    @netojose

    Yeah you should be able to do so:

    @Table()
    export class Post {
        @PrimaryGeneratedColumn()
        id: number;
    
        @OneToOne(type => Members, member=>member.post)
        @JoinColumn({ name: "member_id" })
        member: Members;
    
       // this is optional:
       // @Column({  type: "int", nullable: true })
       // memberId: number;
    
        @Column()
        comment:string;
    }

    @pleerock , thanks for the comment. I hate the typeorm documentation, has no search feature, and is very hard to find you are looking for

  5. itseramin commented on Feb 14, 2024

    @itseramin

    How would one do the same thing with an enum? The migration generator thinks it's a new column and needs to be created.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions