Open thiagomini opened 1 year ago
Just to add some more context:
That situation is quite common in many applications - we have the same table (and almost always the user
table) that represents different entities from different Bounded Contexts. In the case above, the user
table represents:
I believe it would be helpful to be able to map that kind of relationship in Mikro-ORM, giving us the power to reuse the same table for different purposes. I know that ultimately this is due to bad data design, but nonetheless, is a pretty common scenario.
EDIT: Pinpointing the issue, it's happening because MikroORM thinks the EmployeeEntity
was not yet persisted - and tries to create it in the database. If we had a way to force Mikro-ORM to set an entity as persisted
, it would fix that error. Alternatively, since the schema is defining that relationship with persist: false
, it should also not try to create a new user.
to reuse the same table for different purposes.
That is called single table inheritance:
https://mikro-orm.io/docs/inheritance-mapping#single-table-inheritance
Or maybe you have a table-per-type inheritance? That's unfortunately not supported.
Hey, Martin, thanks for the reply! However, my use case is similar but isn't precisely table inheritance. There are two main differences:
user
and employee
entities do not have any inheritance relationship at all. The same row in the database represents two different things depending on the context - either an employee or an IAM user.Basically, it's not about inheritance, it's about a set of attributes and relationships being used in one context, while another set of attributes and relationships are used in a different context. As I've mentioned, imagine the example:
user
table record:
id: 1
firstName: John
lastName: Doe
email: john@doe.com
companyId: 1
However, when we think about the user
entity in an IAM context, this is what matters:
IAM User:
id: 1
email: john@doe.com
roles: ['admin']
On the other hand, in the Company Management context, these are the attributes/relationships that matter: Employee User:
id: 1
email: john@doe.com
company: Acme
details: {
salary: 10_000
}
So, the same user row is represented by a different set of attributes depending on the Bounded Context. That modeling strategy makes it easier to map only the necessary relationships and attributes for each context instead of reusing the same User
table schema for everthing.
Describe the bug
TLDR
User
entity class with its schemaEmployee
entity class that shares some fields with theUser
(and resides in the same table)EmployeeDetail
class and entity with a One-To-One reference toEmployee
EmployeeDetail
entity, passing theemployee
as an attributeinsert into "user" ("id") values (default) returning "id" - null value in column "email" violates not-null constraint
Consider the following entity relationships:
Also, consider that we have an
Employee
entity that is not described in a table above. That happens because anEmployee
is a "sub-type" ofUser
, with the association with a company:⚠️ When we try to create a new
EmployeeDetail
in the database, pointing to a reference ofEmployee
, Mikro-ORM throws an error:insert into "user" ("id") values (default) returning "id" - null value in column "email" violates not-null constraint
Stack trace
To Reproduce Steps to reproduce the behavior:
yarn
docker compose up -d
yarn test
Expected behavior A new
employee_detail
record should be created, given that we have the existinguser
in the database already.Additional context You can look at the schemas in the repo linked, but here's both the
employee
andemployee_detail
schemas:Employee Schema
EmployeeDetail Schema:
Versions