Skip to content

Commit

Permalink
fix: prevent unique index identical to primary key (all sql dialects) (
Browse files Browse the repository at this point in the history
…#9940)

Co-authored-by: Lucian Mocanu <lucian.mocanu@nexontis.com>
  • Loading branch information
alumni and Lucian Mocanu committed Apr 15, 2023
1 parent 159c60a commit 51eecc2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 29 deletions.
51 changes: 24 additions & 27 deletions src/metadata-builder/RelationJoinColumnBuilder.ts
Expand Up @@ -34,7 +34,7 @@ import { DriverUtils } from "../driver/DriverUtils"
* ])
*
* Since for many-to-one relations having JoinColumn decorator is not required,
* we need to go thought each many-to-one relation without join column decorator set
* we need to go through each many-to-one relation without join column decorator set
* and create join column metadata args for them.
*/
export class RelationJoinColumnBuilder {
Expand Down Expand Up @@ -80,42 +80,39 @@ export class RelationJoinColumnBuilder {
entityMetadata: relation.entityMetadata,
referencedEntityMetadata: relation.inverseEntityMetadata,
namingStrategy: this.connection.namingStrategy,
columns: columns,
referencedColumns: referencedColumns,
columns,
referencedColumns,
onDelete: relation.onDelete,
onUpdate: relation.onUpdate,
deferrable: relation.deferrable,
})

// Oracle does not allow both primary and unique constraints on the same column
// Postgres can't take the unique und primary at once during create and primary key is unique anyway
// SQL requires UNIQUE/PK constraints on columns referenced by a FK
// Skip creating the unique constraint for the referenced columns if
// they are already contained in the PK of the referenced entity
if (
["oracle", "postgres"].includes(
this.connection.driver.options.type,
) &&
columns.every((column) => column.isPrimary)
)
columns.every((column) => column.isPrimary) ||
!relation.isOneToOne
) {
return { foreignKey, columns, uniqueConstraint: undefined }

// CockroachDB requires UNIQUE constraints on referenced columns
if (referencedColumns.length > 0 && relation.isOneToOne) {
const uniqueConstraint = new UniqueMetadata({
entityMetadata: relation.entityMetadata,
columns: foreignKey.columns,
args: {
name: this.connection.namingStrategy.relationConstraintName(
relation.entityMetadata.tableName,
foreignKey.columns.map((c) => c.databaseName),
),
target: relation.entityMetadata.target,
},
})
uniqueConstraint.build(this.connection.namingStrategy)
return { foreignKey, columns, uniqueConstraint }
}

return { foreignKey, columns, uniqueConstraint: undefined }
const uniqueConstraint = new UniqueMetadata({
entityMetadata: relation.entityMetadata,
columns: foreignKey.columns,
args: {
name: this.connection.namingStrategy.relationConstraintName(
relation.entityMetadata.tableName,
foreignKey.columns.map((column) => column.databaseName),
),
target: relation.entityMetadata.target,
},
})
uniqueConstraint.build(this.connection.namingStrategy)

return { foreignKey, columns, uniqueConstraint }
}

// -------------------------------------------------------------------------
// Protected Methods
// -------------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions test/github-issues/8485/issue-8485.ts
Expand Up @@ -7,13 +7,13 @@ import { UserProfile } from "./entity/UserProfile"
import { User } from "./entity/User"
import { expect } from "chai"

describe("github issues > #8485 second migration is generated for a combination of PrimaryColumn and JoinColumn with Postgres", () => {
describe("github issues > #8485 second migration is generated for a combination of PrimaryColumn and JoinColumn", () => {
let dataSources: DataSource[]
before(
async () =>
(dataSources = await createTestingConnections({
entities: [User, UserProfile],
enabledDrivers: ["postgres"],
enabledDrivers: ["mariadb", "mysql", "oracle", "postgres"],
dropSchema: true,
schemaCreate: false,
})),
Expand Down

0 comments on commit 51eecc2

Please sign in to comment.