New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Entity with multiple @ManyToOne's with primary:true causes extra indexes to be generated #760
Comments
Will need to verify, but i believe the indexes were needed. If not needed, then definitely something you should want anyway.
https://stackoverflow.com/questions/970562/postgres-and-indexes-on-foreign-keys-and-primary-keys |
In the top-voted reply, https://stackoverflow.com/a/970605/2612450, it says:
which supports what I was trying to explain in the issue description. The reply that you quoted is correct. Foreign keys are not automatically indexed by default. Any column(s) marked as a primary key, even if they are foreign, are automatically indexed and given a unique constraint. |
Another good point in the reply that you linked to is that creating an index on a foreign key should not be forced. Whether you decide for the default MikroORM behavior to be opt-in or opt-out is up to you, but the end user should be able to disable indexes with
|
Makes sense. This originated probably with MySQL implementation where indexes are required for FKs (at least afaik). |
Ok I can confirm this is not needed for composite keys, not even for MySQL or SQLite. |
Describe the bug
When generating a migration for an entity with multiple
@ManyToOne
properties that are also primary keys, an extra index for each@ManyToOne
column is generated. Using theOrderItem
entity from the example on https://mikro-orm.io/docs/composite-keys/#use-case-3-join-table-with-metadata the following is generated:The last line which adds the primary key is enough to create a unique constraint + index. The indexes on individual columns are not needed. I tried adding
{ index: false }
to the@ManyToOne
options but it still resulted in the individual indexes being generated.Expected behavior
Only the "add constraint" SQL for composite primary key should be present, with no indexes on order_id and product_id.
Versions
The text was updated successfully, but these errors were encountered: