Skip to content

Commit b8704f8

Browse files
authored
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
1 parent 1a9b9fb commit b8704f8

File tree

4 files changed

+63
-1
lines changed

4 files changed

+63
-1
lines changed

src/metadata-builder/RelationJoinColumnBuilder.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,11 @@ export class RelationJoinColumnBuilder {
8888
})
8989

9090
// Oracle does not allow both primary and unique constraints on the same column
91+
// Postgres can't take the unique und primary at once during create and primary key is unique anyway
9192
if (
92-
this.connection.driver.options.type === "oracle" &&
93+
["oracle", "postgres"].includes(
94+
this.connection.driver.options.type,
95+
) &&
9396
columns.every((column) => column.isPrimary)
9497
)
9598
return { foreignKey, columns, uniqueConstraint: undefined }
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Entity, OneToOne, PrimaryGeneratedColumn } from "../../../../src"
2+
import { UserProfile } from "./UserProfile"
3+
4+
@Entity()
5+
export class User {
6+
@PrimaryGeneratedColumn()
7+
public id: number
8+
9+
@OneToOne(() => UserProfile, (userProfile) => userProfile.user)
10+
public profile: UserProfile
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Entity, JoinColumn, OneToOne, PrimaryColumn } from "../../../../src"
2+
import { User } from "./User"
3+
4+
@Entity()
5+
export class UserProfile {
6+
@PrimaryColumn()
7+
public userId: number
8+
9+
@OneToOne(() => User, (user) => user.profile)
10+
@JoinColumn()
11+
public user: User
12+
}

test/github-issues/8485/issue-8485.ts

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { DataSource } from "../../../src"
2+
import {
3+
createTestingConnections,
4+
closeTestingConnections,
5+
} from "../../utils/test-utils"
6+
import { UserProfile } from "./entity/UserProfile"
7+
import { User } from "./entity/User"
8+
import { expect } from "chai"
9+
10+
describe("github issues > #8485 second migration is generated for a combination of PrimaryColumn and JoinColumn with Postgres", () => {
11+
let dataSources: DataSource[]
12+
before(
13+
async () =>
14+
(dataSources = await createTestingConnections({
15+
entities: [User, UserProfile],
16+
enabledDrivers: ["postgres"],
17+
dropSchema: true,
18+
schemaCreate: false,
19+
})),
20+
)
21+
after(() => closeTestingConnections(dataSources))
22+
23+
it("should not create second migration", () =>
24+
Promise.all(
25+
dataSources.map(async (dataSource) => {
26+
await dataSource.driver.createSchemaBuilder().build()
27+
28+
const sqlInMemory = await dataSource.driver
29+
.createSchemaBuilder()
30+
.log()
31+
32+
expect(sqlInMemory.upQueries).to.be.empty
33+
expect(sqlInMemory.downQueries).to.be.empty
34+
}),
35+
))
36+
})

0 commit comments

Comments
 (0)