Skip to content

Conversation

@AlexMesser
Copy link
Collaborator

@AlexMesser AlexMesser commented Apr 16, 2022

Description of change

Includes changes from #8524, fixes issues with existing custom constraint names (indices, uniques) and adds support for custom primary key constraint names.

Custom FK names:

@Entity()
export class Post {
    @ManyToOne((type) => Category)
    @JoinColumn({
        name: "cat_id",
        referencedColumnName: "name",
        foreignKeyConstraintName: "fk_cat_id"
    })
    category: Category
}
@Entity()
export class Post {
    @ManyToMany((type) => Category)
    @JoinTable({
        name: "question_categories",
        joinColumn: {
            name: "question",
            referencedColumnName: "id",
            foreignKeyConstraintName: "fk_question_categories_questionId"
        },
        inverseJoinColumn: {
            name: "category",
            referencedColumnName: "id",
            foreignKeyConstraintName: "fk_question_categories_categoryId"
        },
    })
    categories: Category[]
}

Custom PK names:

@Entity()
export class User {
    @PrimaryColumn({ primaryKeyConstraintName: "pk_user_id" })
    id: number
}
@Entity()
export class User {
    @Column({ primary: true, primaryKeyConstraintName: "pk_user_id" })
    id: number
}
@Entity()
export class User {
    @PrimaryGeneratedColumn({ primaryKeyConstraintName: "pk_user_id" })
    id: number
}

Fixes #1355
Fixes #4572
Fixes #8321

Closes #8524

Pull-Request Checklist

  • Code is up-to-date with the master branch
  • npm run format to apply prettier formatting
  • npm run test passes with this change
  • This pull request links relevant issues as Fixes #0000
  • There are new or updated unit tests validating the change
  • Documentation has been updated to reflect this change
  • The new commits follow conventions explained in CONTRIBUTING.md

M-TGH and others added 12 commits January 17, 2022 14:29
Add a constraintName to JoinColumn decorators to allow specifying foreignKey name.
Use constraintName when building JoinTable entities as well.

Partially solves: #1355
Add constraintName property with correct variable undefined to snapshot in tests for issue 5444.
# Conflicts:
#	docs/decorator-reference.md
#	src/decorator/options/JoinColumnOptions.ts
#	src/decorator/relations/JoinColumn.ts
#	src/entity-schema/EntitySchemaTransformer.ts
#	src/metadata-args/JoinColumnMetadataArgs.ts
#	src/metadata-builder/JunctionEntityMetadataBuilder.ts
#	src/metadata/ColumnMetadata.ts
#	src/metadata/ForeignKeyMetadata.ts
#	test/github-issues/5444/issue-5444.ts
@EPecherkin
Copy link

Beautiful

@rstoughton
Copy link

This would be huge for my team's current migration. Much love ❤️

@eporomaa
Copy link

eporomaa commented May 2, 2022

Much appreciated!

@jeniasaigak
Copy link

Is there a way to pass primaryKeyConstraintName using EntitySchema?

export const UserSchema = new EntitySchema({
  name: 'User',
  columns: {
    id: {
      primary: true,
      type: 'uuid',
      // primaryKeyConstraintName: 'PK_user_id', // <--- There is no such key 😔
    },
  },
});

Thanks in advance!

@jeniasaigak
Copy link

Is there a way to pass primaryKeyConstraintName using EntitySchema?

export const UserSchema = new EntitySchema({
  name: 'User',
  columns: {
    id: {
      primary: true,
      type: 'uuid',
      // primaryKeyConstraintName: 'PK_user_id', // <--- There is no such key 😔
    },
  },
});

Thanks in advance!

Should be resolved at #9309
Thanks, @AlexMesser 🙏

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

Labels

None yet

Projects

None yet

7 participants