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

JOINED loading strategy seems to fail in some cases #1902

Closed
pphunor72 opened this issue Jun 3, 2021 · 3 comments
Closed

JOINED loading strategy seems to fail in some cases #1902

pphunor72 opened this issue Jun 3, 2021 · 3 comments
Assignees
Labels
bug Something isn't working

Comments

@pphunor72
Copy link

I have the following user-tenant entity:

export enum UserRole {
	ADMIN = 'admin',
	USER = 'user'
}

@Entity({ tableName: 'public.pgo_user_tenant' })
@Filter({ name: 'byUser', cond: args => ({ user: { id: args.id } }) })
@Filter({ name: 'byTenant', cond: args => ({ tenant: { id: args.id } }) })
export class UserTenantEntity {

	@ManyToOne({ primary: true, entity: () => UserEntity, fieldName: 'userId', cascade: [] })
	user!: UserEntity;

	@ManyToOne({ primary: true, entity: () => TenantEntity, fieldName: 'tenantId', cascade: [] })
	tenant!: TenantEntity;

	[PrimaryKeyType]: [number, number];

	@Property({ fieldName: 'isActive' })
	isActive: boolean = true;

	@Enum({ items: () => UserRole, default: UserRole.USER })
	role!: UserRole; // string enum
}

Querying this entity does not work properly with joined loading strategy. In my database there are two records with the same user id (1) but different tenant ids (1 and 2). Running the query below works fine, both records are returned:

userTenantRepository.findAll({
			filters: {
				byUser: { id: 1 }
			},
			populate: {
				tenant: true
			},
			//strategy: LoadStrategy.JOINED
		}

If I uncomment the loading strategy entry above then only the first record is returned. The same happens if I set the loading strategy within @manytoone decorator.
It is worth noting that even in these cases the SQL statement returns both two records, the issue is somewhere around mapJoinedProps (see @mikro-orm/knex/AbstractSqlDriver.js), because getCompositeKeyHash utility does not recognize the embedded tenant object as entity and the generated hash is

1~~~[Object object]

for both two records (1 is the user id).
Do I failed to add some property somewhere or is this really a buggy behavior?

Versions

Dependency Version
node 14
typescript yes
mikro-orm 4.5.5
your-driver postgresql
@B4nan
Copy link
Member

B4nan commented Jun 3, 2021

Could you please provide a failing test case like in https://github.com/mikro-orm/mikro-orm/tree/master/tests/issues?

@pphunor72
Copy link
Author

Sure, here you are:
GH1902.test.ts.zip

@B4nan B4nan added bug Something isn't working and removed needs clarification labels Jun 4, 2021
@B4nan B4nan closed this as completed in 5412913 Jun 4, 2021
B4nan added a commit that referenced this issue Jun 4, 2021
@B4nan
Copy link
Member

B4nan commented Jun 4, 2021

4.5.6-dev.11

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