Skip to content

Commit

Permalink
fix(schema): do not add unique constraint to PKs
Browse files Browse the repository at this point in the history
PK is unique by definition, so the unique constrain is not needed.

Closes #1064
  • Loading branch information
B4nan committed Nov 13, 2020
1 parent 523b048 commit a7da03d
Show file tree
Hide file tree
Showing 7 changed files with 8 additions and 16 deletions.
6 changes: 5 additions & 1 deletion packages/knex/src/schema/DatabaseTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,12 @@ export class DatabaseTable {
});

this.getColumns().forEach(column => this.getPropertyDeclaration(column, namingStrategy, schemaHelper, compositeFkIndexes, schema));
const meta = schema.init().meta;
meta.relations
.filter(prop => prop.primary && prop.reference === ReferenceType.MANY_TO_ONE && !meta.compositePK)
.forEach(prop => prop.reference = ReferenceType.ONE_TO_ONE);

return schema.init().meta;
return meta;
}

private getPropertyDeclaration(column: Column, namingStrategy: NamingStrategy, schemaHelper: SchemaHelper, compositeFkIndexes: Dictionary<{ keyName: string }>, schema: EntitySchema) {
Expand Down
2 changes: 1 addition & 1 deletion packages/knex/src/schema/SchemaGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ export class SchemaGenerator {
Utils.runIfNotEmpty(() => col.primary(), prop.primary && !meta.compositePK);
Utils.runIfNotEmpty(() => col.unsigned(), pkProp.unsigned);
Utils.runIfNotEmpty(() => col.index(indexName), !composite && index);
Utils.runIfNotEmpty(() => col.unique(uniqueName), !composite && prop.unique);
Utils.runIfNotEmpty(() => col.unique(uniqueName), !composite && prop.unique && (!prop.primary || meta.compositePK));
Utils.runIfNotEmpty(() => col.defaultTo(prop.defaultRaw ? this.knex.raw(prop.defaultRaw) : null), !sameDefault);
Utils.runIfNotEmpty(() => col.comment(prop.comment!), prop.comment);

Expand Down
4 changes: 2 additions & 2 deletions tests/__snapshots__/EntityGenerator.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Author2 } from './Author2';
@Entity()
export class Address2 {
@OneToOne({ entity: () => Author2, cascade: [Cascade.ALL], primary: true, index: 'address2_author_id_index', unique: 'address2_author_id_unique' })
@OneToOne({ entity: () => Author2, cascade: [Cascade.ALL], primary: true, index: 'address2_author_id_index' })
author!: Author2;
@Property({ length: 255 })
Expand Down Expand Up @@ -521,7 +521,7 @@ import { Author2 } from './Author2';
@Entity()
export class Address2 {
@OneToOne({ entity: () => Author2, cascade: [Cascade.ALL], primary: true, unique: 'address2_author_id_unique' })
@OneToOne({ entity: () => Author2, cascade: [Cascade.ALL], primary: true })
author!: Author2;
@Property({ length: 255 })
Expand Down
4 changes: 0 additions & 4 deletions tests/__snapshots__/Migrator.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ export class Migration20191013214813 extends Migration {
this.addSql('create table \`address2\` (\`author_id\` int(11) unsigned not null, \`value\` varchar(255) not null comment \\\\'This is address property\\\\') default character set utf8mb4 engine = InnoDB comment = \\\\'This is address table\\\\';');
this.addSql('alter table \`address2\` add primary key \`address2_pkey\`(\`author_id\`);');
this.addSql('alter table \`address2\` add index \`address2_author_id_index\`(\`author_id\`);');
this.addSql('alter table \`address2\` add unique \`address2_author_id_unique\`(\`author_id\`);');
this.addSql('alter table \`foo_bar2\` add constraint \`foo_bar2_baz_id_foreign\` foreign key (\`baz_id\`) references \`foo_baz2\` (\`id\`) on update cascade on delete set null;');
this.addSql('alter table \`foo_bar2\` add constraint \`foo_bar2_foo_bar_id_foreign\` foreign key (\`foo_bar_id\`) references \`foo_bar2\` (\`id\`) on update cascade on delete set null;');
Expand Down Expand Up @@ -290,7 +289,6 @@ export class Migration20191013214813 extends Migration {
"create table \`address2\` (\`author_id\` int(11) unsigned not null, \`value\` varchar(255) not null comment 'This is address property') default character set utf8mb4 engine = InnoDB comment = 'This is address table';",
"alter table \`address2\` add primary key \`address2_pkey\`(\`author_id\`);",
"alter table \`address2\` add index \`address2_author_id_index\`(\`author_id\`);",
"alter table \`address2\` add unique \`address2_author_id_unique\`(\`author_id\`);",
"",
"alter table \`foo_bar2\` add constraint \`foo_bar2_baz_id_foreign\` foreign key (\`baz_id\`) references \`foo_baz2\` (\`id\`) on update cascade on delete set null;",
"alter table \`foo_bar2\` add constraint \`foo_bar2_foo_bar_id_foreign\` foreign key (\`foo_bar_id\`) references \`foo_bar2\` (\`id\`) on update cascade on delete set null;",
Expand Down Expand Up @@ -467,7 +465,6 @@ export class Migration20191013214813 extends Migration {
this.addSql('create table \`address2\` (\`author_id\` int(11) unsigned not null, \`value\` varchar(255) not null comment \\\\'This is address property\\\\') default character set utf8mb4 engine = InnoDB comment = \\\\'This is address table\\\\';');
this.addSql('alter table \`address2\` add primary key \`address2_pkey\`(\`author_id\`);');
this.addSql('alter table \`address2\` add index \`address2_author_id_index\`(\`author_id\`);');
this.addSql('alter table \`address2\` add unique \`address2_author_id_unique\`(\`author_id\`);');
this.addSql('alter table \`foo_bar2\` add constraint \`foo_bar2_baz_id_foreign\` foreign key (\`baz_id\`) references \`foo_baz2\` (\`id\`) on update cascade on delete set null;');
this.addSql('alter table \`foo_bar2\` add constraint \`foo_bar2_foo_bar_id_foreign\` foreign key (\`foo_bar_id\`) references \`foo_bar2\` (\`id\`) on update cascade on delete set null;');
Expand Down Expand Up @@ -635,7 +632,6 @@ export class Migration20191013214813 extends Migration {
"create table \`address2\` (\`author_id\` int(11) unsigned not null, \`value\` varchar(255) not null comment 'This is address property') default character set utf8mb4 engine = InnoDB comment = 'This is address table';",
"alter table \`address2\` add primary key \`address2_pkey\`(\`author_id\`);",
"alter table \`address2\` add index \`address2_author_id_index\`(\`author_id\`);",
"alter table \`address2\` add unique \`address2_author_id_unique\`(\`author_id\`);",
"",
"alter table \`foo_bar2\` add constraint \`foo_bar2_baz_id_foreign\` foreign key (\`baz_id\`) references \`foo_baz2\` (\`id\`) on update cascade on delete set null;",
"alter table \`foo_bar2\` add constraint \`foo_bar2_foo_bar_id_foreign\` foreign key (\`foo_bar_id\`) references \`foo_bar2\` (\`id\`) on update cascade on delete set null;",
Expand Down
6 changes: 0 additions & 6 deletions tests/__snapshots__/SchemaGenerator.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ alter table \`author2_following\` add primary key \`author2_following_pkey\`(\`a
create table \`address2\` (\`author_id\` int(11) unsigned not null, \`value\` varchar(255) not null comment 'This is address property') default character set utf8mb4 engine = InnoDB comment = 'This is address table';
alter table \`address2\` add primary key \`address2_pkey\`(\`author_id\`);
alter table \`address2\` add index \`address2_author_id_index\`(\`author_id\`);
alter table \`address2\` add unique \`address2_author_id_unique\`(\`author_id\`);
alter table \`foo_bar2\` add constraint \`foo_bar2_baz_id_foreign\` foreign key (\`baz_id\`) references \`foo_baz2\` (\`id\`) on update cascade on delete set null;
alter table \`foo_bar2\` add constraint \`foo_bar2_foo_bar_id_foreign\` foreign key (\`foo_bar_id\`) references \`foo_bar2\` (\`id\`) on update cascade on delete set null;
Expand Down Expand Up @@ -332,7 +331,6 @@ alter table \`author2_following\` add primary key \`author2_following_pkey\`(\`a
create table \`address2\` (\`author_id\` int(11) unsigned not null, \`value\` varchar(255) not null comment 'This is address property') default character set utf8mb4 engine = InnoDB comment = 'This is address table';
alter table \`address2\` add primary key \`address2_pkey\`(\`author_id\`);
alter table \`address2\` add index \`address2_author_id_index\`(\`author_id\`);
alter table \`address2\` add unique \`address2_author_id_unique\`(\`author_id\`);
alter table \`foo_bar2\` add constraint \`foo_bar2_baz_id_foreign\` foreign key (\`baz_id\`) references \`foo_baz2\` (\`id\`) on update cascade on delete set null;
alter table \`foo_bar2\` add constraint \`foo_bar2_foo_bar_id_foreign\` foreign key (\`foo_bar_id\`) references \`foo_bar2\` (\`id\`) on update cascade on delete set null;
Expand Down Expand Up @@ -449,7 +447,6 @@ create table \\"address2\\" (\\"author_id\\" int4 not null, \\"value\\" varchar(
comment on table \\"address2\\" is 'This is address table';
comment on column \\"address2\\".\\"value\\" is 'This is address property';
alter table \\"address2\\" add constraint \\"address2_pkey\\" primary key (\\"author_id\\");
alter table \\"address2\\" add constraint \\"address2_author_id_unique\\" unique (\\"author_id\\");
create table \\"book2\\" (\\"uuid_pk\\" varchar(36) not null, \\"created_at\\" timestamptz(3) not null default current_timestamp(3), \\"title\\" varchar(255) null default '', \\"perex\\" text null, \\"price\\" float null, \\"double\\" double precision null, \\"meta\\" jsonb null, \\"author_id\\" int4 not null, \\"publisher_id\\" int4 null);
alter table \\"book2\\" add constraint \\"book2_pkey\\" primary key (\\"uuid_pk\\");
Expand Down Expand Up @@ -596,7 +593,6 @@ create table \\"address2\\" (\\"author_id\\" int4 not null, \\"value\\" varchar(
comment on table \\"address2\\" is 'This is address table';
comment on column \\"address2\\".\\"value\\" is 'This is address property';
alter table \\"address2\\" add constraint \\"address2_pkey\\" primary key (\\"author_id\\");
alter table \\"address2\\" add constraint \\"address2_author_id_unique\\" unique (\\"author_id\\");
create table \\"book2\\" (\\"uuid_pk\\" varchar(36) not null, \\"created_at\\" timestamptz(3) not null default current_timestamp(3), \\"title\\" varchar(255) null default '', \\"perex\\" text null, \\"price\\" float null, \\"double\\" double precision null, \\"meta\\" jsonb null, \\"author_id\\" int4 not null, \\"publisher_id\\" int4 null);
alter table \\"book2\\" add constraint \\"book2_pkey\\" primary key (\\"uuid_pk\\");
Expand Down Expand Up @@ -1065,7 +1061,6 @@ alter table \`author2_following\` add primary key \`author2_following_pkey\`(\`a
create table \`address2\` (\`author_id\` int(11) unsigned not null, \`value\` varchar(255) not null comment 'This is address property') default character set utf8mb4 engine = InnoDB comment = 'This is address table';
alter table \`address2\` add primary key \`address2_pkey\`(\`author_id\`);
alter table \`address2\` add index \`address2_author_id_index\`(\`author_id\`);
alter table \`address2\` add unique \`address2_author_id_unique\`(\`author_id\`);
alter table \`foo_bar2\` add constraint \`foo_bar2_baz_id_foreign\` foreign key (\`baz_id\`) references \`foo_baz2\` (\`id\`) on update cascade on delete set null;
alter table \`foo_bar2\` add constraint \`foo_bar2_foo_bar_id_foreign\` foreign key (\`foo_bar_id\`) references \`foo_bar2\` (\`id\`) on update cascade on delete set null;
Expand Down Expand Up @@ -1166,7 +1161,6 @@ create table \\"address2\\" (\\"author_id\\" int4 not null, \\"value\\" varchar(
comment on table \\"address2\\" is 'This is address table';
comment on column \\"address2\\".\\"value\\" is 'This is address property';
alter table \\"address2\\" add constraint \\"address2_pkey\\" primary key (\\"author_id\\");
alter table \\"address2\\" add constraint \\"address2_author_id_unique\\" unique (\\"author_id\\");
create table \\"book2\\" (\\"uuid_pk\\" varchar(36) not null, \\"created_at\\" timestamptz(3) not null default current_timestamp(3), \\"title\\" varchar(255) null default '', \\"perex\\" text null, \\"price\\" float null, \\"double\\" double precision null, \\"meta\\" jsonb null, \\"author_id\\" int4 not null, \\"publisher_id\\" int4 null);
alter table \\"book2\\" add constraint \\"book2_pkey\\" primary key (\\"uuid_pk\\");
Expand Down
1 change: 0 additions & 1 deletion tests/mysql-schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ alter table `author2_following` add primary key `author2_following_pkey`(`author
create table `address2` (`author_id` int(11) unsigned not null, `value` varchar(255) not null comment 'This is address property') default character set utf8mb4 engine = InnoDB comment = 'This is address table';
alter table `address2` add primary key `address2_pkey`(`author_id`);
alter table `address2` add index `address2_author_id_index`(`author_id`);
alter table `address2` add unique `address2_author_id_unique`(`author_id`);

alter table `foo_bar2` add constraint `foo_bar2_baz_id_foreign` foreign key (`baz_id`) references `foo_baz2` (`id`) on update cascade on delete set null;
alter table `foo_bar2` add constraint `foo_bar2_foo_bar_id_foreign` foreign key (`foo_bar_id`) references `foo_bar2` (`id`) on update cascade on delete set null;
Expand Down
1 change: 0 additions & 1 deletion tests/postgre-schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ create table "address2" ("author_id" int4 not null, "value" varchar(255) not nul
comment on table "address2" is 'This is address table';
comment on column "address2"."value" is 'This is address property';
alter table "address2" add constraint "address2_pkey" primary key ("author_id");
alter table "address2" add constraint "address2_author_id_unique" unique ("author_id");

create table "book2" ("uuid_pk" varchar(36) not null, "created_at" timestamptz(3) not null default current_timestamp(3), "title" varchar(255) null default '', "perex" text null, "price" float null, "double" numeric null, "meta" jsonb null, "author_id" int4 not null, "publisher_id" int4 null, "foo" varchar(255) null default 'lol');
alter table "book2" add constraint "book2_pkey" primary key ("uuid_pk");
Expand Down

0 comments on commit a7da03d

Please sign in to comment.