Skip to content
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

Many-to-Many association with Custom Pivot Entity fails when one of the Entities' class name is non-standard #4988

Closed
5 tasks done
thiagomini opened this issue Dec 4, 2023 · 1 comment

Comments

@thiagomini
Copy link
Contributor

Describe the bug

  • Given we have a relationship of Many-to-Many: A Company has many ProductEntity's , and a ProductEntity may belong to many Company's
  • And the associations were defined using entity schemas
  • And There is a custom pivot entity called CompanyProduct
  • When we create a new Company and add a new Product to its collection
  • Then MikroORM throws an error: InvalidFieldNameException: insert into "test"."company_product" ("company_id", "product_entity_id") values ('1', '1') returning "id" - column "product_entity_id" of relation "company_product" does not exist

Full Stack trace:

src/mikro-orm.test.ts > Mikro Orm > creates a company with products
InvalidFieldNameException: insert into "test"."company_product" ("company_id", "product_entity_id") values ('1', '1') returning "id" - column "product_entity_id" of relation "company_product" does not exist
 ❯ PostgreSqlExceptionConverter.convertException node_modules/@mikro-orm/postgresql/PostgreSqlExceptionConverter.js:36:24
 ❯ PostgreSqlDriver.convertException node_modules/@mikro-orm/core/drivers/DatabaseDriver.js:201:54
 ❯ node_modules/@mikro-orm/core/drivers/DatabaseDriver.js:205:24
 ❯ PostgreSqlDriver.nativeInsertMany node_modules/@mikro-orm/knex/AbstractSqlDriver.js:300:21
 ❯ PostgreSqlDriver.updateCollectionDiff node_modules/@mikro-orm/knex/AbstractSqlDriver.js:742:13
 ❯ UnitOfWork.persistToDatabase node_modules/@mikro-orm/core/unit-of-work/UnitOfWork.js:715:13
 ❯ PostgreSqlConnection.transactional node_modules/@mikro-orm/knex/AbstractSqlConnection.js:36:25
 ❯ UnitOfWork.doCommit node_modules/@mikro-orm/core/unit-of-work/UnitOfWork.js:295:17
 ❯ UnitOfWork.commit node_modules/@mikro-orm/core/unit-of-work/UnitOfWork.js:268:13
 ❯ Parser.parseErrorMessage node_modules/pg-protocol/src/parser.ts:369:69
 ❯ Parser.handlePacket node_modules/pg-protocol/src/parser.ts:188:21
 ❯ Parser.parse node_modules/pg-protocol/src/parser.ts:103:30

Reproduction

  1. Clone the minimum reproducible code repository: https://github.com/thiagomini/mikro-orm-repro/tree/bug/many-to-many-association
  2. run yarn
  3. run docker compose up -d
  4. run yarn test

What driver are you using?

@mikro-orm/postgresql

MikroORM version

5.9.4

Node.js version

20.9.0

Operating system

WSL 2.0

Validations

@B4nan
Copy link
Member

B4nan commented Dec 21, 2023

While the issue is valid, the repro won't work after the fix on its own - you can't use onCreate property option for pivot entities here, working with the M:N relation does not create such entity instances under the hood, so they won't be executed. You need a db default (defaultRaw: 'current_timestamp'), and similarly, pivot rows are never updated, only added or removed, so the onUpdate will only work if you manually load that pivot entity and modify it.

edit: and of course the fix itself is not enough for v5, there is some weird issue with the discovery apparenlty

edit2: actually nope, it's just the EntitySchema type not being strict enough which is already addressed in v6 luckily, ported to 5.x branch too

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants