Skip to content

Commit

Permalink
fix: redundant Unique constraint on primary join column in Postgres (#…
Browse files Browse the repository at this point in the history
…9677)

* test: one migration for PrimaryColumn and JoinColumn in pg

* fix: stop postgres from creating unique on PrimaryColumn with JoinColumn
  • Loading branch information
KiwiKilian committed Feb 6, 2023
1 parent 1a9b9fb commit b8704f8
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/metadata-builder/RelationJoinColumnBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,11 @@ export class RelationJoinColumnBuilder {
})

// 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
if (
this.connection.driver.options.type === "oracle" &&
["oracle", "postgres"].includes(
this.connection.driver.options.type,
) &&
columns.every((column) => column.isPrimary)
)
return { foreignKey, columns, uniqueConstraint: undefined }
Expand Down
11 changes: 11 additions & 0 deletions test/github-issues/8485/entity/User.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Entity, OneToOne, PrimaryGeneratedColumn } from "../../../../src"
import { UserProfile } from "./UserProfile"

@Entity()
export class User {
@PrimaryGeneratedColumn()
public id: number

@OneToOne(() => UserProfile, (userProfile) => userProfile.user)
public profile: UserProfile
}
12 changes: 12 additions & 0 deletions test/github-issues/8485/entity/UserProfile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Entity, JoinColumn, OneToOne, PrimaryColumn } from "../../../../src"
import { User } from "./User"

@Entity()
export class UserProfile {
@PrimaryColumn()
public userId: number

@OneToOne(() => User, (user) => user.profile)
@JoinColumn()
public user: User
}
36 changes: 36 additions & 0 deletions test/github-issues/8485/issue-8485.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { DataSource } from "../../../src"
import {
createTestingConnections,
closeTestingConnections,
} from "../../utils/test-utils"
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", () => {
let dataSources: DataSource[]
before(
async () =>
(dataSources = await createTestingConnections({
entities: [User, UserProfile],
enabledDrivers: ["postgres"],
dropSchema: true,
schemaCreate: false,
})),
)
after(() => closeTestingConnections(dataSources))

it("should not create second migration", () =>
Promise.all(
dataSources.map(async (dataSource) => {
await dataSource.driver.createSchemaBuilder().build()

const sqlInMemory = await dataSource.driver
.createSchemaBuilder()
.log()

expect(sqlInMemory.upQueries).to.be.empty
expect(sqlInMemory.downQueries).to.be.empty
}),
))
})

0 comments on commit b8704f8

Please sign in to comment.