objection.js icon indicating copy to clipboard operation
objection.js copied to clipboard

Inserting into a table with no ID column gives `Invalid column name 'id'`

Open KrisLau opened this issue 3 years ago • 8 comments

I keep getting this error whenever I try to insert using objection: insert into [job_execution] ([last_run_at], [name]) output inserted.[id] values (@p0, @p1) - Invalid column name 'id'.

Code:

await JobExecution.query().insert([{
    name: 'test'
    last_run_at: new Date().toISOString(),
}]);

I've also tried it without the array (just a single object). The same error does not occur when adding the record through knex. Changing the migration file to include an ID column fixes the error but I don't need an id field.

KrisLau avatar Jul 22 '22 13:07 KrisLau

How is your model?

caiquecastro avatar Jul 25 '22 18:07 caiquecastro

How is your model?

It's just:

class JobExecution extends Model {
    static get tableName() {
        return 'job_execution';
    }

    // static methods for getting and updating the table
}

KrisLau avatar Jul 25 '22 19:07 KrisLau

Do you have a custom primary key column? You can customize it on your model:

https://vincit.github.io/objection.js/recipes/custom-id-column.html

caiquecastro avatar Jul 26 '22 17:07 caiquecastro

Do you have a custom primary key column? You can customize it on your model:

https://vincit.github.io/objection.js/recipes/custom-id-column.html

Can I not have the table without a primary key column? I have another table that doesn't have one and this table is copied directly from that but only this one is having problems

KrisLau avatar Jul 26 '22 17:07 KrisLau

Hi @KrisLau, is this issue still happening? I tried to reproduce and I didn't get the same result.

grazirs avatar Aug 04 '22 18:08 grazirs

@grazirs It is yeah but I'm unsure what is causing it. I have another table that doesn't have an ID but it's not having a problem and all the code for this table is copied exactly from that one.

KrisLau avatar Aug 04 '22 18:08 KrisLau

All tables should include the primary key, and the objection insert script is not working without the primary key on my end. But I dont think its the issue on the library, because in DBA, we have to use the indexing even if we design the relation table for the M:N relationship

iSeanWeiDev avatar Aug 08 '22 16:08 iSeanWeiDev

All tables should include the primary key, and the objection insert script is not working without the primary key on my end. But I dont think its the issue on the library, because in DBA, we have to use the indexing even if we design the relation table for the M:N relationship

@iSeanWeiDev But for some reason it is allowing one table without a primary key and not allowing another

KrisLau avatar Aug 08 '22 17:08 KrisLau