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

feat: allow explicitly named primary keys, foreign keys, and indices #8900

Merged
merged 17 commits into from
Apr 29, 2022

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
6 participants