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

Wrapping a query with the "$and" operator causes a invalid query #849

Closed
merceyz opened this issue Sep 17, 2020 · 2 comments
Closed

Wrapping a query with the "$and" operator causes a invalid query #849

merceyz opened this issue Sep 17, 2020 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@merceyz
Copy link
Contributor

merceyz commented Sep 17, 2020

Describe the bug

Wrapping a find query with the $and operator causes it to query the wrong table+column

Stack trace

InvalidFieldNameException: select `e0`.*, `e1`.`id` as `user_id` from `profile` as `e0` left join `user` as `e1` on `e0`.`id` = `e1`.`profile_id` where `e0`.`user_id` in (123) - SQLITE_ERROR: no such column: e0.user_id
    at SqliteExceptionConverter.convertException (C:\testcase\.yarn\$$virtual\@mikro-orm-sqlite-virtual-4d94b4ca8a\0\cache\@mikro-orm-sqlite-npm-4.0.3-6abbc1e0ec-39975ba0c2.zip\node_modules\@mikro-orm\sqlite\SqliteExceptionConverter.js:32:20)
    at SqliteDriver.convertException (C:\testcase\.yarn\$$virtual\@mikro-orm-core-virtual-6c2ac32c4b\0\cache\@mikro-orm-core-npm-4.0.3-4ae154529f-9ed736faf4.zip\node_modules\@mikro-orm\core\drivers\DatabaseDriver.js:168:54)
    at C:\testcase\.yarn\$$virtual\@mikro-orm-core-virtual-6c2ac32c4b\0\cache\@mikro-orm-core-npm-4.0.3-4ae154529f-9ed736faf4.zip\node_modules\@mikro-orm\core\drivers\DatabaseDriver.js:172:24
    at SqliteDriver.find (C:\testcase\.yarn\$$virtual\@mikro-orm-knex-virtual-5180ec5ee4\0\cache\@mikro-orm-knex-npm-4.0.3-43ff3bffe1-ed619bd1a1.zip\node_modules\@mikro-orm\knex\AbstractSqlDriver.js:42:24)
    at SqlEntityManager.find (C:\testcase\.yarn\$$virtual\@mikro-orm-core-virtual-6c2ac32c4b\0\cache\@mikro-orm-core-npm-4.0.3-4ae154529f-9ed736faf4.zip\node_modules\@mikro-orm\core\EntityManager.js:75:25)
    at C:\testcase\index.ts:24:2

To Reproduce

// User.ts
import { Entity, PrimaryKey, OneToOne } from '@mikro-orm/core';
import { Profile } from './Profile';

@Entity()
export class User {
	@PrimaryKey()
	id: number;

	@OneToOne(() => Profile)
	profile: Profile;
}

// Profile.ts
import { Entity, PrimaryKey, OneToOne } from '@mikro-orm/core';
import { User } from './User';

@Entity()
export class Profile {
	@PrimaryKey()
	id: number;

	@OneToOne(() => User, (user) => user.profile)
	user: User;
}

// index.ts
import { MikroORM } from '@mikro-orm/core';
import { Profile } from './Profile';
import { User } from './User';

(async () => {
	const orm = await MikroORM.init({
		type: 'sqlite',
		dbName: ':memory:',
		debug: true,
		entities: [User, Profile],
	});

	await orm.getSchemaGenerator().ensureDatabase();
	await orm.getSchemaGenerator().createSchema();

	const user = new User();
	user.id = 123;
	user.profile = new Profile();
	user.profile.id = 456;
	await orm.em.persist(user).flush();
	orm.em.clear();

	// OK
	await orm.em.getRepository(Profile).find({ user: { id: [123] } });
	// Error
	await orm.em.getRepository(Profile).find({ $and: [{ user: { id: [123] } }] });
})();

Expected behavior

Wrapping the query in $and should not cause it to fail

Versions

Dependency Version
node 12.18.3
typescript 4.0.2
@mikro-orm/core 4.0.3
@mikro-orm/sqlite 4.0.3
@mikro-orm/mysql 4.0.3
@merceyz merceyz added the bug Something isn't working label Sep 17, 2020
@B4nan
Copy link
Member

B4nan commented Sep 17, 2020

This works fine btw:

await orm.em.getRepository(Profile).find({ $and: [{ user: { id: { $in: [123] } } }] });

@B4nan
Copy link
Member

B4nan commented Sep 18, 2020

So looks like that query I suggested above "works", but it is actually also wrong (searches by the inverse side PK instead)...

@B4nan B4nan closed this as completed in a46281e Sep 18, 2020
B4nan added a commit that referenced this issue Sep 19, 2020
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