question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

createOneBase missing id in result when entity have composite primary columns

See original GitHub issue

I 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:open
  • Created 4 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
m33chcommented, Nov 28, 2019

Hi, try to change id definition:

@Column({ type: 'int' })
id: number

to :

@PrimaryGeneratedColumn()
id: number
0reactions
cncoldercommented, Dec 18, 2019

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found