symfony throws message “couldn’t get last insert identifier”
I’ve started using the Symfony PHP framework for my php projects, and while the learning curve has been steeper than I would have liked, I’m getting there.
Anyway, one of the problems I’ve had a few times over the current project is the error
“couldn’t get last insert identifier”
So far, the problem has been down to the object I have been trying to create didn’t have it’s id field set to autoincrement. Or perhaps more accurately, setting that model’s id field to autoincrement has sorted the problem so far.
Hopefully once I get the hang of the schema file and the (what appears to be) the slightly different syntax for migrations, all my models should have autoincrement id fields and this shouldn’t be a problem anymore.
Make sure you have designated a primary key -
In your schema.yml – something like:
columns:
id: { type: integer(4), notnull: true, autoincrement: false, primary: true }
This was my problem, too. I changed database field to be auto_increment and the problem was fixed. Although I didn’t yet discovered why that field suddenly wasn’t auto_incremented.
This post worked for me. Many thanks