fix: redundant Unique constraint on primary join column in Postgres (#9677)

* test: one migration for PrimaryColumn and JoinColumn in pg

* fix: stop postgres from creating unique on PrimaryColumn with JoinColumn
This commit is contained in:
Kilian Finger 2023-02-06 14:28:07 +01:00 committed by GitHub
parent 1a9b9fbcd6
commit b8704f87d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 63 additions and 1 deletions

View File

@ -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 }

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}),
))
})