Comments (9)
I hope the fix wont break anything else :D
from mikro-orm.
You don't have to redefine anything, just define the joinColumn
on the parent. Redefining relations seems like a bad idea on its own to me.
from mikro-orm.
I literally started crafting a minimal example to create a bug report like this at the same time you were creating yours!
@carlosV2 small world :-)
from mikro-orm.
A workaround is to redefine the @ManyToMany
relationship within each child entity and explicitly define the join column:
@Entity({ discriminatorColumn: "type" })
class User {
@ManyToMany({ entity: () => Privilege })
privileges = new Collection<Privilege>(this);
}
@Entity()
class SuperUser extends User {
@ManyToMany({ entity: () => Privilege, joinColumn: "user_id" })
privileges = new Collection<Privilege>(this);
}
@Entity()
class AdminUser extends User {
@ManyToMany({ entity: () => Privilege, joinColumn: "user_id" })
privileges = new Collection<Privilege>(this);
}
from mikro-orm.
I literally started crafting a minimal example to create a bug report like this at the same time you were creating yours!
Anyway, if anyone is interested, this is the minimal example: https://github.com/carlosV2/mo-test
The idea on the example is to have a parent Book entity and 2 child entities extending it (PaperbackBook and HardcoverBook). The name of the books is a many-to-many with Text which has a language field to provide translations.
This example is a super-simplification of a real-life code I'm currently working on.
You can run npm run db
to provide yourself a PostgreSQL DB (via docker) and then run npm run app
to get the SQL printed in screen.
In my case it yields the following:
create table "book" ("id" text not null, "pages" int not null, "cover" text check ("cover" in ('paperback', 'hardcover')) not null, constraint "book_pkey" primary key ("id"));
create index "book_cover_index" on "book" ("cover");
create table "text" ("id" text not null, "language" text check ("language" in ('en', 'es')) not null, "content" text not null, constraint "text_pkey" primary key ("id"));
create table "book_name" ("hardcover_book_id" text not null, "text_id" text not null, constraint "book_name_pkey" primary key ("hardcover_book_id", "text_id"));
alter table "book_name" add constraint "book_name_hardcover_book_id_foreign" foreign key ("hardcover_book_id") references "book" ("id") on update cascade on delete cascade;
alter table "book_name" add constraint "book_name_text_id_foreign" foreign key ("text_id") references "text" ("id") on update cascade on delete cascade;
Notice how the book_name
table has the hardcover_book_id
while it should be just book_id
.
from mikro-orm.
@B4nan thanks for the improved workaround. I just confirmed that works on the test repo, in my actual project, which uses the PostgreSQL driver, and @carlosV2' repo. I thought that I had already tried that approach and it didn't work, but it seems that there were some other issues in my code that made it fail.
That said, it's still a workaround. I think that the default behavior is unexpected and undesirable.
from mikro-orm.
Of course, it broke one test and I managed to miss that locally, its gonna be a bit harder.
from mikro-orm.
Thank you for jumping on the fix so quickly, @B4nan! I love MikroORM, thanks for all the work you put into it.
from mikro-orm.
Ok, refactored via a132036. The fix was technically breaking, as now we infer the join column names based on the base type, but the previous behavior didn't make much sense and if people want it, they should override this explicitly.
from mikro-orm.
Related Issues (20)
- [Bug]: Formula set as ScalarReference not loaded as LoadedScalarReference at runtime HOT 6
- matching doesn't set _populated to true HOT 8
- Order by doesnโt work with embeddable fields inside populated entity if embedabble has object true
- Assigning a customType and flushing to database does not escape returning values HOT 1
- Join gets removed when the populate doesn't include the nested orderBy relation
- Migration schema diffing fails with deleteRule: 'set null ("...")'
- docs: Multiple schemas, but not all schemas
- Mongodb driver doesn't detect deep updates HOT 1
- Utils.copy() does not maintain the new instance metadata
- Populate invalid cast date
- Allow hydration of virtual properties in embeddables HOT 14
- [Bug]: Loaded type does not catch missing population types when Collection is in `populate` array HOT 7
- I can't use MySQL driver in MariaDB
- mikro-orm migration:fresh --drop-db runs migrations against wrong db
- 'longblob' property generates `varchar(255)` in the snapshot HOT 3
- Timezone issue
- Support PostgreSQL parameterized queries HOT 1
- Fetching partial entities with MongoDB does not work
- findOne/findOneOrFail should work without where param or with empty where param HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mikro-orm.