Skip to content

Commit

Permalink
fix(schema): do not create indexes for each composite PK
Browse files Browse the repository at this point in the history
Closes: #760
  • Loading branch information
B4nan committed Aug 20, 2020
1 parent fe6862a commit 91b38cb
Show file tree
Hide file tree
Showing 8 changed files with 210 additions and 216 deletions.
2 changes: 1 addition & 1 deletion packages/knex/src/schema/SchemaGenerator.ts
Expand Up @@ -373,7 +373,7 @@ export class SchemaGenerator {
const nullable = (alter && this.platform.requiresNullableForAlteringColumn()) || prop.nullable!;
const sameNullable = alter && 'sameNullable' in alter && alter.sameNullable;
const indexed = 'index' in prop ? prop.index : (prop.reference !== ReferenceType.SCALAR && this.helper.indexForeignKeys());
const index = (indexed || (prop.primary && meta.compositePK)) && !(alter?.sameIndex);
const index = indexed && !alter?.sameIndex;
const indexName = this.getIndexName(meta, prop, false, columnName);
const uniqueName = this.getIndexName(meta, prop, true, columnName);
const sameDefault = alter && 'sameDefault' in alter ? alter.sameDefault : !prop.defaultRaw;
Expand Down
37 changes: 15 additions & 22 deletions tests/__snapshots__/EntityGenerator.test.ts.snap
Expand Up @@ -117,12 +117,6 @@ export class BaseUser2 {
@Property({ columnType: 'enum' })
type!: string;
@Property({ nullable: true })
employeeProp?: number;
@Property({ length: 255, nullable: true })
managerProp?: string;
@Property({ length: 255, nullable: true })
ownerProp?: string;
Expand All @@ -132,6 +126,12 @@ export class BaseUser2 {
@OneToOne({ entity: () => BaseUser2, nullable: true, index: 'base_user2_favourite_manager_id_index', unique: 'base_user2_favourite_manager_id_unique' })
favouriteManager?: BaseUser2;
@Property({ nullable: true })
employeeProp?: number;
@Property({ length: 255, nullable: true })
managerProp?: string;
}
",
"import { Cascade, Entity, ManyToOne, PrimaryKey, Property } from '@mikro-orm/core';
Expand Down Expand Up @@ -254,13 +254,12 @@ export class CarOwner2 {
}
",
"import { Entity, Index, ManyToOne, PrimaryKey, Property } from '@mikro-orm/core';
"import { Entity, ManyToOne, PrimaryKey, Property } from '@mikro-orm/core';
import { Test2 } from './Test2';
@Entity()
export class Configuration2 {
@Index({ name: 'configuration2_property_index' })
@PrimaryKey({ length: 255 })
property!: string;
Expand Down Expand Up @@ -440,17 +439,15 @@ export class Test2 {
}
",
"import { Entity, Index, OneToOne, PrimaryKey, Property } from '@mikro-orm/core';
"import { Entity, OneToOne, PrimaryKey, Property } from '@mikro-orm/core';
import { Car2 } from './Car2';
@Entity()
export class User2 {
@Index({ name: 'user2_first_name_index' })
@PrimaryKey({ length: 100 })
firstName!: string;
@Index({ name: 'user2_last_name_index' })
@PrimaryKey({ length: 100 })
lastName!: string;
Expand Down Expand Up @@ -679,17 +676,16 @@ export class BookToTagUnordered {
}
",
"import { Entity, Index, ManyToOne, PrimaryKey, Property } from '@mikro-orm/core';
"import { Entity, ManyToOne, PrimaryKey, Property } from '@mikro-orm/core';
import { Test2 } from './Test2';
@Entity()
export class Configuration2 {
@Index({ name: 'configuration2_property_index' })
@PrimaryKey({ length: 255 })
property!: string;
@ManyToOne({ entity: () => Test2, primary: true, index: 'configuration2_test_id_index' })
@ManyToOne({ entity: () => Test2, primary: true })
test!: Test2;
@Property({ length: 255 })
Expand Down Expand Up @@ -752,10 +748,10 @@ import { FooBaz2 } from './FooBaz2';
@Entity()
export class FooParam2 {
@ManyToOne({ entity: () => FooBar2, primary: true, index: 'foo_param2_bar_id_index' })
@ManyToOne({ entity: () => FooBar2, primary: true })
bar!: FooBar2;
@ManyToOne({ entity: () => FooBaz2, primary: true, index: 'foo_param2_baz_id_index' })
@ManyToOne({ entity: () => FooBaz2, primary: true })
baz!: FooBaz2;
@Property({ length: 255 })
Expand Down Expand Up @@ -881,13 +877,13 @@ export class Author3 {
@Property()
termsAccepted!: number;
@Property({ nullable: true })
@Property({ columnType: 'text', nullable: true })
identities?: string;
@Property({ columnType: 'date', nullable: true })
@Property({ columnType: 'date(3)', nullable: true })
born?: string;
@Property({ columnType: 'time', nullable: true })
@Property({ columnType: 'time(3)', nullable: true })
bornTime?: string;
@ManyToOne({ entity: () => Book3, nullable: true, index: 'author3_favourite_book_id_index' })
Expand All @@ -914,9 +910,6 @@ export class Book3 {
@Property({ default: '' })
title!: string;
@Property({ nullable: true, default: 'lol' })
foo?: string;
@ManyToOne({ entity: () => Author3, nullable: true, index: 'book3_author_id_index' })
author?: Author3;
Expand Down
21 changes: 0 additions & 21 deletions tests/__snapshots__/SchemaGenerator.test.ts.snap
Expand Up @@ -32,8 +32,6 @@ create table \`car_owner2\` (\`id\` int unsigned not null auto_increment primary
alter table \`car_owner2\` add index \`car_owner2_car_name_car_year_index\`(\`car_name\`, \`car_year\`);
create table \`user2\` (\`first_name\` varchar(100) not null, \`last_name\` varchar(100) not null, \`foo\` int(11) null, \`favourite_car_name\` varchar(100) null, \`favourite_car_year\` int(11) unsigned null) default character set utf8mb4 engine = InnoDB;
alter table \`user2\` add index \`user2_first_name_index\`(\`first_name\`);
alter table \`user2\` add index \`user2_last_name_index\`(\`last_name\`);
alter table \`user2\` add unique \`user2_favourite_car_name_unique\`(\`favourite_car_name\`);
alter table \`user2\` add unique \`user2_favourite_car_year_unique\`(\`favourite_car_year\`);
alter table \`user2\` add primary key \`user2_pkey\`(\`first_name\`, \`last_name\`);
Expand Down Expand Up @@ -79,7 +77,6 @@ alter table \`test2\` add index \`test2_book_uuid_pk_index\`(\`book_uuid_pk\`);
alter table \`test2\` add unique \`test2_book_uuid_pk_unique\`(\`book_uuid_pk\`);
create table \`configuration2\` (\`property\` varchar(255) not null, \`test_id\` int(11) unsigned not null, \`value\` varchar(255) not null) default character set utf8mb4 engine = InnoDB;
alter table \`configuration2\` add index \`configuration2_property_index\`(\`property\`);
alter table \`configuration2\` add index \`configuration2_test_id_index\`(\`test_id\`);
alter table \`configuration2\` add primary key \`configuration2_pkey\`(\`property\`, \`test_id\`);
Expand Down Expand Up @@ -249,8 +246,6 @@ create table \`car_owner2\` (\`id\` int unsigned not null auto_increment primary
alter table \`car_owner2\` add index \`car_owner2_car_name_car_year_index\`(\`car_name\`, \`car_year\`);
create table \`user2\` (\`first_name\` varchar(100) not null, \`last_name\` varchar(100) not null, \`foo\` int(11) null, \`favourite_car_name\` varchar(100) null, \`favourite_car_year\` int(11) unsigned null) default character set utf8mb4 engine = InnoDB;
alter table \`user2\` add index \`user2_first_name_index\`(\`first_name\`);
alter table \`user2\` add index \`user2_last_name_index\`(\`last_name\`);
alter table \`user2\` add unique \`user2_favourite_car_name_unique\`(\`favourite_car_name\`);
alter table \`user2\` add unique \`user2_favourite_car_year_unique\`(\`favourite_car_year\`);
alter table \`user2\` add primary key \`user2_pkey\`(\`first_name\`, \`last_name\`);
Expand Down Expand Up @@ -296,7 +291,6 @@ alter table \`test2\` add index \`test2_book_uuid_pk_index\`(\`book_uuid_pk\`);
alter table \`test2\` add unique \`test2_book_uuid_pk_unique\`(\`book_uuid_pk\`);
create table \`configuration2\` (\`property\` varchar(255) not null, \`test_id\` int(11) unsigned not null, \`value\` varchar(255) not null) default character set utf8mb4 engine = InnoDB;
alter table \`configuration2\` add index \`configuration2_property_index\`(\`property\`);
alter table \`configuration2\` add index \`configuration2_test_id_index\`(\`test_id\`);
alter table \`configuration2\` add primary key \`configuration2_pkey\`(\`property\`, \`test_id\`);
Expand Down Expand Up @@ -408,8 +402,6 @@ alter table \\"foo_bar2\\" add constraint \\"foo_bar2_baz_id_unique\\" unique (\
alter table \\"foo_bar2\\" add constraint \\"foo_bar2_foo_bar_id_unique\\" unique (\\"foo_bar_id\\");
create table \\"foo_param2\\" (\\"bar_id\\" int4 not null, \\"baz_id\\" int4 not null, \\"value\\" varchar(255) not null);
create index \\"foo_param2_bar_id_index\\" on \\"foo_param2\\" (\\"bar_id\\");
create index \\"foo_param2_baz_id_index\\" on \\"foo_param2\\" (\\"baz_id\\");
alter table \\"foo_param2\\" add constraint \\"foo_param2_pkey\\" primary key (\\"bar_id\\", \\"baz_id\\");
create table \\"publisher2\\" (\\"id\\" serial primary key, \\"name\\" varchar(255) not null, \\"type\\" text check (\\"type\\" in ('local', 'global')) not null, \\"type2\\" text check (\\"type2\\" in ('LOCAL', 'GLOBAL')) not null, \\"enum1\\" int2 null, \\"enum2\\" int2 null, \\"enum3\\" int2 null, \\"enum4\\" text check (\\"enum4\\" in ('a', 'b', 'c')) null);
Expand Down Expand Up @@ -439,8 +431,6 @@ create table \\"test2\\" (\\"id\\" serial primary key, \\"name\\" varchar(255) n
alter table \\"test2\\" add constraint \\"test2_book_uuid_pk_unique\\" unique (\\"book_uuid_pk\\");
create table \\"configuration2\\" (\\"property\\" varchar(255) not null, \\"test_id\\" int4 not null, \\"value\\" varchar(255) not null);
create index \\"configuration2_property_index\\" on \\"configuration2\\" (\\"property\\");
create index \\"configuration2_test_id_index\\" on \\"configuration2\\" (\\"test_id\\");
alter table \\"configuration2\\" add constraint \\"configuration2_pkey\\" primary key (\\"property\\", \\"test_id\\");
create table \\"publisher2_tests\\" (\\"id\\" serial primary key, \\"publisher2_id\\" int4 not null, \\"test2_id\\" int4 not null);
Expand Down Expand Up @@ -549,8 +539,6 @@ alter table \\"foo_bar2\\" add constraint \\"foo_bar2_baz_id_unique\\" unique (\
alter table \\"foo_bar2\\" add constraint \\"foo_bar2_foo_bar_id_unique\\" unique (\\"foo_bar_id\\");
create table \\"foo_param2\\" (\\"bar_id\\" int4 not null, \\"baz_id\\" int4 not null, \\"value\\" varchar(255) not null);
create index \\"foo_param2_bar_id_index\\" on \\"foo_param2\\" (\\"bar_id\\");
create index \\"foo_param2_baz_id_index\\" on \\"foo_param2\\" (\\"baz_id\\");
alter table \\"foo_param2\\" add constraint \\"foo_param2_pkey\\" primary key (\\"bar_id\\", \\"baz_id\\");
create table \\"publisher2\\" (\\"id\\" serial primary key, \\"name\\" varchar(255) not null, \\"type\\" text check (\\"type\\" in ('local', 'global')) not null, \\"type2\\" text check (\\"type2\\" in ('LOCAL', 'GLOBAL')) not null, \\"enum1\\" int2 null, \\"enum2\\" int2 null, \\"enum3\\" int2 null, \\"enum4\\" text check (\\"enum4\\" in ('a', 'b', 'c')) null);
Expand Down Expand Up @@ -580,8 +568,6 @@ create table \\"test2\\" (\\"id\\" serial primary key, \\"name\\" varchar(255) n
alter table \\"test2\\" add constraint \\"test2_book_uuid_pk_unique\\" unique (\\"book_uuid_pk\\");
create table \\"configuration2\\" (\\"property\\" varchar(255) not null, \\"test_id\\" int4 not null, \\"value\\" varchar(255) not null);
create index \\"configuration2_property_index\\" on \\"configuration2\\" (\\"property\\");
create index \\"configuration2_test_id_index\\" on \\"configuration2\\" (\\"test_id\\");
alter table \\"configuration2\\" add constraint \\"configuration2_pkey\\" primary key (\\"property\\", \\"test_id\\");
create table \\"publisher2_tests\\" (\\"id\\" serial primary key, \\"publisher2_id\\" int4 not null, \\"test2_id\\" int4 not null);
Expand Down Expand Up @@ -935,8 +921,6 @@ create table \`car_owner2\` (\`id\` int unsigned not null auto_increment primary
alter table \`car_owner2\` add index \`car_owner2_car_name_car_year_index\`(\`car_name\`, \`car_year\`);
create table \`user2\` (\`first_name\` varchar(100) not null, \`last_name\` varchar(100) not null, \`foo\` int(11) null, \`favourite_car_name\` varchar(100) null, \`favourite_car_year\` int(11) unsigned null) default character set utf8mb4 engine = InnoDB;
alter table \`user2\` add index \`user2_first_name_index\`(\`first_name\`);
alter table \`user2\` add index \`user2_last_name_index\`(\`last_name\`);
alter table \`user2\` add unique \`user2_favourite_car_name_unique\`(\`favourite_car_name\`);
alter table \`user2\` add unique \`user2_favourite_car_year_unique\`(\`favourite_car_year\`);
alter table \`user2\` add primary key \`user2_pkey\`(\`first_name\`, \`last_name\`);
Expand Down Expand Up @@ -982,7 +966,6 @@ alter table \`test2\` add index \`test2_book_uuid_pk_index\`(\`book_uuid_pk\`);
alter table \`test2\` add unique \`test2_book_uuid_pk_unique\`(\`book_uuid_pk\`);
create table \`configuration2\` (\`property\` varchar(255) not null, \`test_id\` int(11) unsigned not null, \`value\` varchar(255) not null) default character set utf8mb4 engine = InnoDB;
alter table \`configuration2\` add index \`configuration2_property_index\`(\`property\`);
alter table \`configuration2\` add index \`configuration2_test_id_index\`(\`test_id\`);
alter table \`configuration2\` add primary key \`configuration2_pkey\`(\`property\`, \`test_id\`);
Expand Down Expand Up @@ -1078,8 +1061,6 @@ alter table \\"foo_bar2\\" add constraint \\"foo_bar2_baz_id_unique\\" unique (\
alter table \\"foo_bar2\\" add constraint \\"foo_bar2_foo_bar_id_unique\\" unique (\\"foo_bar_id\\");
create table \\"foo_param2\\" (\\"bar_id\\" int4 not null, \\"baz_id\\" int4 not null, \\"value\\" varchar(255) not null);
create index \\"foo_param2_bar_id_index\\" on \\"foo_param2\\" (\\"bar_id\\");
create index \\"foo_param2_baz_id_index\\" on \\"foo_param2\\" (\\"baz_id\\");
alter table \\"foo_param2\\" add constraint \\"foo_param2_pkey\\" primary key (\\"bar_id\\", \\"baz_id\\");
create table \\"publisher2\\" (\\"id\\" serial primary key, \\"name\\" varchar(255) not null, \\"type\\" text check (\\"type\\" in ('local', 'global')) not null, \\"type2\\" text check (\\"type2\\" in ('LOCAL', 'GLOBAL')) not null, \\"enum1\\" int2 null, \\"enum2\\" int2 null, \\"enum3\\" int2 null, \\"enum4\\" text check (\\"enum4\\" in ('a', 'b', 'c')) null);
Expand Down Expand Up @@ -1109,8 +1090,6 @@ create table \\"test2\\" (\\"id\\" serial primary key, \\"name\\" varchar(255) n
alter table \\"test2\\" add constraint \\"test2_book_uuid_pk_unique\\" unique (\\"book_uuid_pk\\");
create table \\"configuration2\\" (\\"property\\" varchar(255) not null, \\"test_id\\" int4 not null, \\"value\\" varchar(255) not null);
create index \\"configuration2_property_index\\" on \\"configuration2\\" (\\"property\\");
create index \\"configuration2_test_id_index\\" on \\"configuration2\\" (\\"test_id\\");
alter table \\"configuration2\\" add constraint \\"configuration2_pkey\\" primary key (\\"property\\", \\"test_id\\");
create table \\"publisher2_tests\\" (\\"id\\" serial primary key, \\"publisher2_id\\" int4 not null, \\"test2_id\\" int4 not null);
Expand Down
6 changes: 6 additions & 0 deletions tests/issues/GH529.test.ts
Expand Up @@ -117,4 +117,10 @@ describe('GH issue 529', () => {
await o.items.init();
expect(o.items.getItems()).toHaveLength(3);
});

test(`GH issue 760`, async () => {
const sql = await orm.getSchemaGenerator().getCreateSchemaSQL();
expect(sql).toMatchSnapshot();
});

});
23 changes: 23 additions & 0 deletions tests/issues/__snapshots__/GH529.test.ts.snap
@@ -0,0 +1,23 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`GH issue 529 GH issue 760 1`] = `
"set names 'utf8';
set session_replication_role = 'replica';
create table \\"product\\" (\\"id\\" serial primary key, \\"name\\" varchar(255) not null, \\"current_price\\" int4 not null);
create table \\"customer\\" (\\"id\\" serial primary key);
create table \\"order\\" (\\"id\\" serial primary key, \\"customer_id\\" int4 not null, \\"paid\\" jsonb not null, \\"shipped\\" jsonb not null, \\"created\\" timestamptz(0) not null);
create table \\"order_item\\" (\\"order_id\\" int4 not null, \\"product_id\\" int4 not null, \\"amount\\" int4 not null, \\"offered_price\\" int4 not null);
alter table \\"order_item\\" add constraint \\"order_item_pkey\\" primary key (\\"order_id\\", \\"product_id\\");
alter table \\"order\\" add constraint \\"order_customer_id_foreign\\" foreign key (\\"customer_id\\") references \\"customer\\" (\\"id\\") on update cascade;
alter table \\"order_item\\" add constraint \\"order_item_order_id_foreign\\" foreign key (\\"order_id\\") references \\"order\\" (\\"id\\") on update cascade;
alter table \\"order_item\\" add constraint \\"order_item_product_id_foreign\\" foreign key (\\"product_id\\") references \\"product\\" (\\"id\\") on update cascade;
set session_replication_role = 'origin';
"
`;

0 comments on commit 91b38cb

Please sign in to comment.