PVermeer / dexie-addon-suite-monorepo

MIT License
8 stars 0 forks source link

[populate-addon] Compound key as primary key #35

Closed Ahmed-Ali closed 1 year ago

Ahmed-Ali commented 1 year ago

Hello Dexie expertes! Thank you for the sweet suite of addons!

As far as I understood, Dexie will treat the first field in the table definition as the primary key. However, in all examples for the populate addon, seems like the foreign key, must be mentioned before it is being used. i.e elem_id => elements.id, [elem_id+page_name] So what if my primary key is combination of two keys [elem_id+page_name], will this work [elem_id+page_name], elem_id => elements.id ?

I tried it out. It did not give errors on creating the database. Did not get to the whole flow yet which will do the CRUD operations, but wanted to double check in case I am not on the right path.

PVermeer commented 1 year ago

Hey Ahmed-Ali,

I never considered this approach. I tested it a bit and I can not guarantee that it will always work. I guess this will work with simple databases but it is not supported by this package. I would always recommend to just use a defined primary key (e.g. auto-incremented) since you can just query with where() for whatever you need (e.g. compound keys). This also keeps your database structure simple and more robust for future changes.

I hope this answers your question.

Closing this for now.