Model with composite primary key


Good afternoon,
how do you implement a model based off of a database table that has no single column primary key? The table has a composite primary key but no unique column. I want to create a model for this table, but as far as I can tell the model requires a unique id field.

Thank you in advance


I suggest you to keep your structure and just add extra primery key ‘id’ for internal edit/delete purpose.


Good afternoon again,
if I do that then I would allow that the same pair of foreign keys can be repeated several times in the table and it can not happen. Because of that i need a composite key as the primary key in the table. Does that mean that the model with composite key can not be implemented?

Thank you in advance



You can always use beforeSave/beforeInsert hook to check your repetation.

If you prefer composit keys managed by database itself, then wrap your coding in try catch and catch exception by PDO to display nice errors in your system for repeated keys.

You can also create Expression to concat your primery keys and chek the expression for existance in beforeSave.


Good afternoon,
thank you very much for your suggestions. They were very useful. :slight_smile: