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

Query is not building properly when using fixedOrder and working with schemas #2621

Closed
victor-alegre-santos opened this issue Jan 10, 2022 · 1 comment
Labels
bug Something isn't working

Comments

@victor-alegre-santos
Copy link

Describe the bug
When using a ManyToMany relation with a schema defined in pivotTable and property fixedOrder is set to true (regardless if fixedOrderColumn is defined or not), the order by part of the built query is not correct.

Stack trace

TableNotFoundException: select "e0".*, "e1"."pivot_field_one" as "fk__pivot_field_one", "e1"."pivot_field_two" as "fk__pivot_field_two" from "schema"."command" as "e0" left join "schema"."pivot_table" as "e1" on "e0"."id" = "e1"."pivot_field_one" where "e1"."pivot_field_two" in (1, 2, 3) order by "schema"."pivot_table" asc - missing FROM-clause entry for table "schema"      
    at PostgreSqlExceptionConverter.convertException (...\node_modules\@mikro-orm\postgresql\PostgreSqlExceptionConverter.js:36:24)
    at PostgreSqlDriver.convertException (...\node_modules\@mikro-orm\core\drivers\DatabaseDriver.js:202:54)
    at ...\node_modules\@mikro-orm\core\drivers\DatabaseDriver.js:206:24
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
    at PostgreSqlDriver.loadFromPivotTable (...\node_modules\@mikro-orm\knex\AbstractSqlDriver.js:364:39)
    at EntityLoader.findChildrenFromPivotTable (...\node_modules\@mikro-orm\core\entity\EntityLoader.js:226:21)
    at EntityLoader.populateField (...\node_modules\@mikro-orm\core\entity\EntityLoader.js:185:25)
    at EntityLoader.populate (...\node_modules\@mikro-orm\core\entity\EntityLoader.js:37:13)     
    at SqlEntityManager.find (...\node_modules\@mikro-orm\core\EntityManager.js:105:9)
    at EntityLoader.populateMany (...\node_modules\@mikro-orm\core\entity\EntityLoader.js:127:22)    at EntityLoader.populateField (...\node_modules\@mikro-orm\core\entity\EntityLoader.js:187:9)    at EntityLoader.populate (...\node_modules\@mikro-orm\core\entity\EntityLoader.js:37:13)     
    at SqlEntityManager.lockAndPopulate (...\node_modules\@mikro-orm\core\EntityManager.js:630:9)    at SqlEntityManager.findOne (...\node_modules\@mikro-orm\core\EntityManager.js:244:9)        
    at SqlEntityManager.findOneOrFail (...\node_modules\@mikro-orm\core\EntityManager.js:254:24) 
    at async Promise.all (index 0)

previous error: select "e0".*, "e1"."pivot_field_one" as "fk__pivot_field_one", "e1"."pivot_field_two" as "fk__pivot_field_two" from "schema"."command" as "e0" left join "schema"."pivot_table" as "e1" on "e0"."id" = "e1"."pivot_field_one" where "e1"."pivot_field_two" in 
(1, 2, 3) order by "schema"."pivot_table" asc - missing FROM-clause entry for table "schema"
    at Parser.parseErrorMessage (...\node_modules\pg-protocol\src\parser.ts:369:69)
    at Parser.handlePacket (...\node_modules\pg-protocol\src\parser.ts:188:21)
    at Parser.parse (...\node_modules\pg-protocol\src\parser.ts:103:30)
    at Socket.<anonymous> (...\node_modules\pg-protocol\src\index.ts:7:48)
    at Socket.emit (events.js:375:28)
    at Socket.emit (domain.js:470:12)
    at addChunk (internal/streams/readable.js:290:12)
    at readableAddChunk (internal/streams/readable.js:265:9)
    at Socket.Readable.push (internal/streams/readable.js:204:10)
    at TCP.onStreamRead (internal/stream_base_commons.js:188:23)
    at TCP.callbackTrampoline (internal/async_hooks.js:131:17)

To Reproduce
Steps to reproduce the behavior:

  1. Create an unidirectional ManyToMany collection
  2. Set pivotTable with schema
  3. Set fixedOrder to true

Expected behavior
The query should be built with a correct order by clause, "e1"."id" asc in this case.

Additional context
I tried it in two ways, neither worked

@ManyToMany({
    entity: () => Class,
    pivotTable: 'schema.pivot_table',
    fixedOrder: true,
    fixedOrderColumn: 'id',
    inverseJoinColumn: 'pivot_field_one'
})
field?: Collection<Class> = new Collection<Class>(this);
@ManyToMany({
    entity: () => Class,
    pivotTable: 'schema.pivot_table',
    fixedOrder: true,
    inverseJoinColumn: 'pivot_field_one'
})
field?: Collection<Class> = new Collection<Class>(this);

Versions

Dependency Version
node 14.17.3
typescript 4.4.4
@mikro-orm/core 4.5.10
@mikro-orm/nestjs 4.3.1
@mikro-orm/postgresql 4.5.10
@victor-alegre-santos victor-alegre-santos changed the title Query is not building properly when fixedOrder is set on a PostgreSQL many to many relation Query is not building properly when using fixedOrder and working with schemas Jan 11, 2022
@B4nan B4nan added the bug Something isn't working label Jan 15, 2022
@B4nan B4nan closed this as completed in eb1f9bb Jan 15, 2022
B4nan added a commit that referenced this issue Jan 15, 2022
Fixes mostly postgres handling multiple schemas and diffing, also fixes diffing
when explicit public schema is provided.

Related: #2621
@B4nan
Copy link
Member

B4nan commented Jan 15, 2022

This was much trickier than expected, unfortunately not backportable to v4.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants