createOneBase missing id in result when entity have composite primary columns
See original GitHub issueI have Exam entity and /exams
controllers.
TypeOrmModule option synchronize: false
.
MySQL table name is exam
and only one default PK id int(11)
.
When I first create exam. It return result without id
.
POST /exams
{"title":"my exam","studentId":148,"paperId":233}
I got partial result:
{
paperId: 233,
studentId: 148,
title: "4个常见知识点",
}
Because the second SELECT
is wrong.
query: SELECT `Exam`.`id` AS `Exam_id`, `Exam`.`title` AS `Exam_title`, `Exam`.`paper_id` AS `Exam_paper_id`, `Exam`.`student_id` AS `Exam_student_id` FROM `exam` `Exam` WHERE `Exam`.`paper_id` = ? AND `Exam`.`student_id` = ? -- PARAMETERS: [233,148]
query: START TRANSACTION
query: INSERT INTO `exam`(`id`, `title`, `paper_id`, `student_id`) VALUES (DEFAULT, ?, ?, ?) -- PARAMETERS: [233,148,148]
query: SELECT `Exam`.`paper_id` AS `Exam_paper_id`, `Exam`.`student_id` AS `Exam_student_id` FROM `exam` `Exam` WHERE `Exam`.`paper_id` = ? AND `Exam`.`student_id` = ? -- PARAMETERS: [233,148]
query: COMMIT
After that I post again
POST /exams
{"title":"my exam 2","studentId":148,"paperId":233}
I got the same result (without id
). But this time SQL Query is correct:
query: SELECT `Exam`.`id` AS `Exam_id`, `Exam`.`title` AS `Exam_title`, `Exam`.`paper_id` AS `Exam_paper_id`, `Exam`.`student_id` AS `Exam_student_id` FROM `exam` `Exam` WHERE `Exam`.`paper_id` = ? AND `Exam`.`student_id` = ? -- PARAMETERS: [233,148]
Below is my Entity and Controller.
@Entity({ name: 'exam' })
export class Exam {
@Column({ type: 'int' })
id: number
@Column({ length: 32 })
title: string
@PrimaryColumn({ name: 'paper_id', type: 'int' })
paperId: number
@PrimaryColumn({ name: 'student_id', type: 'int' })
studentId: number
}
@Crud({
model: {
type: Exam,
},
query: {
// persist: ['id'], <-- not works
},
})
@Controller('exams')
export class ExamsController implements CrudController<Exam> {
constructor(readonly service: ExamsService) {}
}
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (3 by maintainers)
Top Results From Across the Web
Composite Primary key, using @IdClass - Column 'id' cannot ...
I am using @IdClass for handling the composite primary key. @Entity @IdClass(MyKey.class) public class YourEntity { @Id ...
Read more >TypeORM - Quick Guide - Tutorialspoint
PrimaryGeneratedColumn() decorator class is used to represent that the id column is the primary key column of the Student entity. Column() decorator class...
Read more >Configuring Entities - CommonsWare
Composite Primary Keys. In some cases, you may have a composite primary key, made up of two or more columns in the database....
Read more >Doctrine 2 - Can't set composite primary key named “id”
But now I have a table, which consists of a composited primary key (of three columns). Two Columns ("id" and "language") are foreign...
Read more >Is a composite primary key a good idea? - Ask TOM
Tom,I have a table that has 3 fields that, when considered in combination, could uniquely ... However, when I looked up composite primary...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hi, try to change id definition:
to :
When I
POST
an exam.{"title":"my exam 2","studentId":148,"paperId":233}
. If{"studentId":148,"paperId":233}
existed.createOne
will update title.But If I define
PrimaryGeneratedColumn
id column.createOne
will create new exam.