Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

emajic's avatar

composite primary keys in eloquent|lumen work?

Alright, from what I have read, including from the laravel creator is that eloquent will never support primary composite keys.

What I do not understand is they seem to work. I use: $table->primary(['one', 'two', 'three'], 'primaryKey');

select index from tablename;

Shows the table with the composite primary key.

+--------------+------------+---------------------------------+--------------+-------------+
| Table        | Non_unique | Key_name                        | Seq_in_index | Column_name |
+--------------+------------+---------------------------------+--------------+-------------+
| tablename |          0 | PRIMARY                         |            1 | one   |
| tablename |          0 | PRIMARY                         |            2 | two  |
| tablename |          0 | PRIMARY                         |            3 | three    |

I am now able to call save on my models, and it correctly throws integrity constraint exceptions on dupes. So why is it working? I am misunderstanding something.....

0 likes
1 reply
cklester's avatar

Try using it with ->save(). Expected behavior is it will fail, because it's not supported there.

Well, I just noticed you said you call save() on your models. How?!!?! :D

Please or to participate in this conversation.