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

spacedog4's avatar

How to make a table with multiples primarykeys auto increment?

So I have this table with two primary keys, id and store_id how do I make the id auto increment based on the store_id

like this

------ -------------
  id   |  store_id
------ -------------
   1    |         1
------ -------------
   2    |         1
------ -------------
   3    |         1
------ -------------
   1    |         2
------ -------------
   2    |         2
-------------------

If it is possible in migration

$table->integer('id');
$table->integer('store_id');

$table->primarykey(['id', 'store_id']);

0 likes
6 replies
Dalma's avatar
Dalma
Best Answer
Level 6

Hi what you are asking for seems more like multi-tenancy where the data is unique by store. If all of the data is valid for a single company or account you would need to use a different structure something like

id   store_id  store_seq
-----------------------------
1     1               1
2     1               2
3     1               3
4     2               1
5     2               2
6     3               1
7     3               2

The id is a record id so it must be unique. The store_id is an identifier for which store is involved The store_seq is a unique counter within each store. This you would need to manually calculate the current maximum value and increment.

1 like
Tray2's avatar

There can only be one primary key in a table but the primary key can be a combination of two fields. Not sure what you are trying to do but I think you should look at your database model and think again.

spacedog4's avatar

@Dalma yeah, that is what I'm doing now, but without the id column I'l explain why in the next answer

spacedog4's avatar

@Tray2 I got this database ready when I enter in this company, for me I would create from scratch

spacedog4's avatar

@jlrdw How I said in the previous answer, I got this database already created when I enter in this company, I would love to reacreate it from scratch using eloquent

Please or to participate in this conversation.