mirror of
https://github.com/typeorm/typeorm.git
synced 2025-12-08 21:26:23 +00:00
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:
parent
1a9b9fbcd6
commit
b8704f87d2
@ -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 }
|
||||
|
||||
11
test/github-issues/8485/entity/User.ts
Normal file
11
test/github-issues/8485/entity/User.ts
Normal 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
|
||||
}
|
||||
12
test/github-issues/8485/entity/UserProfile.ts
Normal file
12
test/github-issues/8485/entity/UserProfile.ts
Normal 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
|
||||
}
|
||||
36
test/github-issues/8485/issue-8485.ts
Normal file
36
test/github-issues/8485/issue-8485.ts
Normal 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
|
||||
}),
|
||||
))
|
||||
})
|
||||
Loading…
x
Reference in New Issue
Block a user