Skip to content

Commit

Permalink
fix(postgres): do not ignore custom PK constraint names
Browse files Browse the repository at this point in the history
Closes #2762
  • Loading branch information
B4nan committed Feb 16, 2022
1 parent 1f7b82b commit 3201ef7
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
4 changes: 4 additions & 0 deletions packages/core/src/platforms/Platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,10 @@ export abstract class Platform {
return this.namingStrategy.indexName(tableName, columns, type);
}

supportsCustomPrimaryKeyNames(): boolean {
return false;
}

shouldHaveColumn<T extends AnyEntity<T>>(prop: EntityProperty<T>, populate: PopulateOptions<T>[] | boolean, includeFormulas = true): boolean {
if (prop.formula) {
return includeFormulas && (!prop.lazy || populate === true || (populate !== false && populate.some(p => p.field === prop.name)));
Expand Down
13 changes: 6 additions & 7 deletions packages/knex/src/schema/SchemaGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,17 +365,17 @@ export class SchemaGenerator extends AbstractSchemaGenerator<AbstractSqlDriver>
}

for (const index of Object.values(diff.addedIndexes)) {
this.createIndex(table, index, diff.name);
this.createIndex(table, index);
}

for (const index of Object.values(diff.changedIndexes)) {
this.createIndex(table, index, diff.name, true);
this.createIndex(table, index, true);
}

for (const [oldIndexName, index] of Object.entries(diff.renamedIndexes)) {
if (index.unique) {
this.dropIndex(table, index, oldIndexName);
this.createIndex(table, index, diff.name);
this.createIndex(table, index);
} else {
this.helper.pushTableQuery(table, this.helper.getRenameIndexSQL(diff.name, index, oldIndexName));
}
Expand Down Expand Up @@ -453,7 +453,7 @@ export class SchemaGenerator extends AbstractSchemaGenerator<AbstractSqlDriver>
});

for (const index of tableDef.getIndexes()) {
this.createIndex(table, index, tableDef.name, !tableDef.getColumns().some(c => c.autoincrement));
this.createIndex(table, index, !tableDef.getColumns().some(c => c.autoincrement));
}

for (const check of tableDef.getChecks()) {
Expand All @@ -474,14 +474,13 @@ export class SchemaGenerator extends AbstractSchemaGenerator<AbstractSqlDriver>
});
}

private createIndex(table: Knex.CreateTableBuilder, index: Index, tableName: string, createPrimary = false) {
private createIndex(table: Knex.CreateTableBuilder, index: Index, createPrimary = false) {
if (index.primary && !createPrimary) {
return;
}

if (index.primary) {
/* istanbul ignore next */
const keyName = tableName.includes('.') ? tableName.split('.').pop()! + '_pkey' : undefined;
const keyName = this.platform.supportsCustomPrimaryKeyNames() ? index.keyName : undefined;
table.primary(index.columnNames, keyName);
} else if (index.unique) {
table.unique(index.columnNames, { indexName: index.keyName });
Expand Down
4 changes: 4 additions & 0 deletions packages/postgresql/src/PostgreSqlPlatform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ export class PostgreSqlPlatform extends AbstractSqlPlatform {
return true;
}

supportsCustomPrimaryKeyNames(): boolean {
return true;
}

/**
* Postgres will complain if we try to batch update uniquely constrained property (moving the value from one entity to another).
* This flag will result in postponing 1:1 updates (removing them from the batched query).
Expand Down

0 comments on commit 3201ef7

Please sign in to comment.