New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
.save() is throwing duplicate key value violates unique constraint on Postgresql #7736
Comments
It looks like you aren't waiting for the Is that the case or is your example incorrect? |
@imnotjames you are right, the example was incorrect, I added the await here (which is present in the original code) ;) |
What is |
This bug was introduced in V0.2.32 |
Since author is willing to help - can you @fabiensabatie please create a PR with a failing test - quite easy to do in this case. If issue exists - it's a breaking change and we need to fix it urgently. |
I'm also encountering this issue, any ETA on a fix? |
Reproducible example: // entities.ts
@Entity()
export class User {
@PrimaryGeneratedColumn()
id: number;
@Column()
name: string;
@OneToMany(() => UserCourse, (course) => course.user)
courses: Promise<UserCourse[]>;
}
@Entity()
export class Course {
@PrimaryGeneratedColumn()
id: number;
@Column()
name: string;
}
@Entity()
export class UserCourse {
@ManyToOne(() => User, (user) => user.courses)
user: Promise<User>;
@PrimaryColumn("int")
userId: number;
@ManyToOne(() => Course)
course: Promise<Course>;
@PrimaryColumn("int")
courseId: number;
@Column()
score: number;
} Then try upserting records: let manager = getManager();
let user = await manager.save(manager.create("User", {name: "user one"}));
let course = await manager.save(manager.create("Course", {name: "course one"}));
// creating record with composite primary key works
await manager.save(manager.create("UserCourse", {userId: user.id, courseId: course.id, score: 4}));
// but updating the record fails:
await manager.save(manager.create("UserCourse", {userId: user.id, courseId: course.id, score: 5})); However if @Entity()
export class UserCourse {
@PrimaryColumn("int")
userId: number;
@PrimaryColumn("int")
courseId: number;
@Column()
score: number;
} then in that case, upsert works as expected And as @leric said, before SQL logs in v0.2.31: query: SELECT "UserCourse"."userId" AS "UserCourse_userId", "UserCourse"."courseId" AS "UserCourse_courseId", "UserCourse"."score" AS "UserCourse_score" FROM "user_course" "UserCourse" WHERE "UserCourse"."userId" = ? AND "UserCourse"."courseId" = ? -- PARAMETERS: [1,1]
query: BEGIN TRANSACTION
query: UPDATE "user_course" SET "score" = ? WHERE "userId" = ? AND "courseId" = ? -- PARAMETERS: [3,1,1]
query: COMMIT SQL logs in v0.2.32 and above: query: SELECT "UserCourse"."userId" AS "UserCourse_userId", "UserCourse"."courseId" AS "UserCourse_courseId", "UserCourse"."score" AS "UserCourse_score" FROM "user_course" "UserCourse" WHERE "UserCourse"."userId" = ? AND "UserCourse"."courseId" = ? -- PARAMETERS: [1,1]
query: SELECT "user"."id" AS "user_id", "user"."name" AS "user_name" FROM "user" "user" INNER JOIN "user_course" "UserCourse" ON "UserCourse"."userId" = "user"."id" WHERE ("UserCourse"."userId" = ? AND "UserCourse"."courseId" = ?) -- PARAMETERS: [1,1]
query: SELECT "course"."id" AS "course_id", "course"."name" AS "course_name" FROM "course" "course" INNER JOIN "user_course" "UserCourse" ON "UserCourse"."courseId" = "course"."id" WHERE ("UserCourse"."userId" = ? AND "UserCourse"."courseId" = ?) -- PARAMETERS: [1,1]
query: BEGIN TRANSACTION
query: INSERT INTO "user_course"("userId", "courseId", "score") VALUES (?, ?, ?) -- PARAMETERS: [1,1,3]
query failed: INSERT INTO "user_course"("userId", "courseId", "score") VALUES (?, ?, ?) -- PARAMETERS: [1,1,3]
error: [Error: SQLITE_CONSTRAINT: UNIQUE constraint failed: user_course.userId, user_course.courseId] {
errno: 19,
code: 'SQLITE_CONSTRAINT'
}
query: ROLLBACK |
The bug was introduced by On L549, Note that this will work as expected for non-lazy relationships |
+1, on sqlite & typeorm@0.2.37
|
+1 on Postgres & 0.2.37 |
+1 on sqlserver & 0.2.37 |
+1 on Postgres 13.3 & typeorm@0.2.37 |
Except the example above does not use lazy loaded relationships. I think there are multiple issues here being conflated together. Perhaps you should open a new issue for that. |
@imnotjames I don't think OP's example is complete. If you try that simple case, it works as expected. That's why I added another example which does use lazy-loaded relationships. As I said earlier, non-lazy-loaded relationships seem to work fine. |
Okay - the original issue was not regarding lazy loaded relationships. Can you synthesize your issue that includes lazy loaded relationships into a new issue? |
I can do that. But is there any other scenario in which this error is thrown? |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
1 similar comment
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
I just created this issue with a code example to reproduce: #8272 |
This comment has been minimized.
This comment has been minimized.
+1 on mongodb@3.7.3 & typeorm@0.2.41 |
getId currently returns undefined for an entity with composite primary key if primary key columns also foreign keys with lazy relations, for e.g., in a custom join table. This commit tries to fix that Closes: typeorm#7736 (maybe)
getId currently returns undefined for an entity with composite primary key if primary key columns also foreign keys with lazy relations, for e.g., in a custom join table. This commit tries to fix that Closes: typeorm#7736 (maybe)
getId currently returns undefined for an entity with composite primary key if primary key columns also foreign keys with lazy relations, for e.g., in a custom join table. This commit tries to fix that Closes: typeorm#7736 (maybe)
getId currently returns undefined for an entity with composite primary key if primary key columns also foreign keys with lazy relations, for e.g., in a custom join table. This commit tries to fix that Closes: typeorm#7736 (maybe)
getId currently returns undefined for an entity with composite primary key if primary key columns also foreign keys with lazy relations, for e.g., in a custom join table. This commit tries to fix that Closes: #7736 (maybe)
any updates? |
+1 TypeOrm 0.2.45 + Postgresql 13 this same bug |
any updates? |
I dont think so... |
Is this fixed yet and if so, in which release? experiencing this with nestjs/typeorm 9.0.1 and with postgres |
Any workaround except removing all lazy relationships ? |
i fixed it but i forgot how lmao |
Is this Fixed? |
+1 this is still happening with
the only workaround for me was to save each part of the relationship individually |
This issue is still happening with: Its possible to use |
For one of the possible solutions you can use the article: PS. Using the save() method only works when you provide ids. |
Issue Description
When trying to upsert an entity to a Postgresql DB, I get a
QueryFailedError: duplicate key value violates unique constraint
.Expected Behavior
I currently have a simple case :
Where the User entity is defined as such :
Actual Behavior
with a
QueryFailedError: duplicate key value violates unique constraint
.Steps to Reproduce
A simple typeorm + Postgresql stack should suffice
My Environment
I'm on a plain TS stack, nothing fancy atm.
Relevant Database Driver(s)
aurora-data-api
aurora-data-api-pg
better-sqlite3
cockroachdb
cordova
expo
mongodb
mysql
nativescript
oracle
postgres
react-native
sap
sqlite
sqlite-abstract
sqljs
sqlserver
Are you willing to resolve this issue by submitting a Pull Request?
The text was updated successfully, but these errors were encountered: