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

Composite join key with metadata example incomplete #529

Closed
willsoto opened this issue Apr 30, 2020 · 1 comment
Closed

Composite join key with metadata example incomplete #529

willsoto opened this issue Apr 30, 2020 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@willsoto
Copy link
Contributor

willsoto commented Apr 30, 2020

Describe the bug
When following the example given here for using a composite PK, the following error is raised when trying to load the related entities:

Trying to query by not existing property OrderItem.order_id

(Note that my code's models are different in name only. I've followed the docs and rechecked all my code extensively, I will be using the naming in the docs here).

The one major(?) difference is that I specify tableName for all my entities, which differs from the auto-generated one. I've also confirmed that the tables themselves are correct.

I was able to verify the queries are correct, so the sql being generated is what I would expect. I also ran the generated sql and it was 👍 . This seems to be an issue with validation in the orm.

Stack trace

Error: Trying to query by not existing property OrderItem.order_id
    at /app/common/temp/node_modules/.registry.npmjs.org/mikro-orm/3.6.12/pg@8.0.3/node_modules/mikro-orm/dist/query/CriteriaNode.js:23:27
    at Array.forEach (<anonymous>)
    at new CriteriaNode (/app/common/temp/node_modules/.registry.npmjs.org/mikro-orm/3.6.12/pg@8.0.3/node_modules/mikro-orm/dist/query/CriteriaNode.js:20:49)
    at new ScalarCriteriaNode (/app/common/temp/node_modules/.registry.npmjs.org/mikro-orm/3.6.12/pg@8.0.3/node_modules/mikro-orm/dist/query/CriteriaNode.js:97:1)
    at Function.create (/app/common/temp/node_modules/.registry.npmjs.org/mikro-orm/3.6.12/pg@8.0.3/node_modules/mikro-orm/dist/query/CriteriaNode.js:99:22)
    at Function.create (/app/common/temp/node_modules/.registry.npmjs.org/mikro-orm/3.6.12/pg@8.0.3/node_modules/mikro-orm/dist/query/CriteriaNode.js:37:35)
    at /app/common/temp/node_modules/.registry.npmjs.org/mikro-orm/3.6.12/pg@8.0.3/node_modules/mikro-orm/dist/query/CriteriaNode.js:156:36
    at Array.reduce (<anonymous>)
    at Function.create (/app/common/temp/node_modules/.registry.npmjs.org/mikro-orm/3.6.12/pg@8.0.3/node_modules/mikro-orm/dist/query/CriteriaNode.js:153:45)
    at Function.create (/app/common/temp/node_modules/.registry.npmjs.org/mikro-orm/3.6.12/pg@8.0.3/node_modules/mikro-orm/dist/query/CriteriaNode.js:35:39)
    at QueryBuilder.orderBy (/app/common/temp/node_modules/.registry.npmjs.org/mikro-orm/3.6.12/pg@8.0.3/node_modules/mikro-orm/dist/query/QueryBuilder.js:101:53)
    at PostgreSqlDriver.find (/app/common/temp/node_modules/.registry.npmjs.org/mikro-orm/3.6.12/pg@8.0.3/node_modules/mikro-orm/dist/drivers/AbstractSqlDriver.js:23:82)
    at EntityManager.find (/app/common/temp/node_modules/.registry.npmjs.org/mikro-orm/3.6.12/pg@8.0.3/node_modules/mikro-orm/dist/EntityManager.js:76:43)
    at Collection.init (/app/common/temp/node_modules/.registry.npmjs.org/mikro-orm/3.6.12/pg@8.0.3/node_modules/mikro-orm/dist/entity/Collection.js:116:32)
    at LibrariesResolver.labs (/app/apps/api/dist/src/libraries/resolver.js:27:29)
    at /app/common/temp/node_modules/.registry.npmjs.org/@nestjs/core/7.0.9/fe769765ff88e6660113be5a6fab38dd/node_modules/@nestjs/core/helpers/external-context-creator.js:68:33

To Reproduce
Steps to reproduce the behavior:

  1. Copy examples from docs
const orderRepo = orm.em.getRepository(Order);

// no errors here at least
await orderRepo.findAll({
  populate: ['items'],
});

// this throws an error
await order.items.init()

Expected behavior
If the entities are configured correctly, no error should be thrown.
If the entities are not configured correctly, a more descriptive error should be thrown.

Additional context
I am using MikroORM via it's NestJS integration.

Some other things I have tried in an effort to get it working, all of them yielding the same error message:

// OrderItem
@ManyToOne({ 
+ // worth noting that MikroORM raises an error if missing `entity` or `type`
+ entity: () => Order,
  primary: true,
+ wrappedReference: true,
})
- order: Order;
+ order!: IdentifiedReference<Order>;

I've also tried specifying fieldName and inversedBy on the OrderItem.order

Versions

Dependency Version
node 12.6.1
typescript 3.8.3
mikro-orm 3.6.12
pg 8.0.3
@willsoto willsoto added the bug Something isn't working label Apr 30, 2020
@rmaes4
Copy link

rmaes4 commented Apr 30, 2020

I am also having this issue

base.entity.ts

@ObjectType()
export abstract class Base {
  @Field((type) => ID)
  @PrimaryKey({ length: 36 })
  uuid = v4();

  @Property()
  createdAt = new Date();

  @Property({ onUpdate: () => new Date() })
  updatedAt = new Date();
}

user.entity.ts

@ObjectType()
@Entity()
export class User extends Base {
  @Unique()
  @Property()
  email!: string;

  @HideField()
  @Property({ length: 60, hidden: true })
  password!: string;

  @Property()
  firstName!: string;

  @Property()
  lastName!: string;

  @Field((type) => [UserToStore])
  @OneToMany(
    () => UserToStore,
    (metaStore) => metaStore.user
  )
  metaStores: Collection<UserToStore> = new Collection<UserToStore>(this);
}

store.entity.ts

@ObjectType()
@Entity()
export class Store extends Base {
  @Property()
  name!: string;

  @Property()
  address?: string;

  @Field((type) => [UserToStore])
  @OneToMany(
    () => UserToStore,
    (metaUser) => metaUser.store
  )
  metaUsers: Collection<UserToStore> = new Collection<UserToStore>(this);
}

user-to-store.entity.ts

export enum UserStoreRole {
  OWNER = 'owner',
  MANAGER = 'manager'
}

registerEnumType(UserStoreRole, {
  name: 'UserStoreRole'
});

@ObjectType()
@Entity()
export class UserToStore {
  @ManyToOne(() => User, { primary: true })
  user!: User;

  @ManyToOne(() => Store, { primary: true })
  store!: Store;

  @Field((type) => UserStoreRole)
  @Enum()
  role!: UserStoreRole;

  [PrimaryKeyType]: [string, string];
}

In my code I call a function like this

async findOne(uuid: string) {
    return this.userRepository.findOne({
      uuid: uuid
    });
  }

Later I call another function using the object returned by the above function

await user.metaStores.init();

And I get this error

[Nest] 73183   - 04/30/2020, 3:39:15 PM   [ExceptionsHandler] Trying to query by not existing property UserToStore.user_uuid +6ms
Error: Trying to query by not existing property UserToStore.user_uuid
    at /Users/robmaes/Documents/ccp/api/node_modules/mikro-orm/dist/query/CriteriaNode.js:23:27
    at Array.forEach (<anonymous>)
    at new CriteriaNode (/Users/robmaes/Documents/ccp/api/node_modules/mikro-orm/dist/query/CriteriaNode.js:20:49)
    at new ScalarCriteriaNode (/Users/robmaes/Documents/ccp/api/node_modules/mikro-orm/dist/query/CriteriaNode.js:97:1)
    at Function.create (/Users/robmaes/Documents/ccp/api/node_modules/mikro-orm/dist/query/CriteriaNode.js:99:22)
    at Function.create (/Users/robmaes/Documents/ccp/api/node_modules/mikro-orm/dist/query/CriteriaNode.js:37:35)
    at /Users/robmaes/Documents/ccp/api/node_modules/mikro-orm/dist/query/CriteriaNode.js:156:36
    at Array.reduce (<anonymous>)
    at Function.create (/Users/robmaes/Documents/ccp/api/node_modules/mikro-orm/dist/query/CriteriaNode.js:153:45)
    at Function.create (/Users/robmaes/Documents/ccp/api/node_modules/mikro-orm/dist/query/CriteriaNode.js:35:39)
    at QueryBuilder.orderBy (/Users/robmaes/Documents/ccp/api/node_modules/mikro-orm/dist/query/QueryBuilder.js:101:53)
    at PostgreSqlDriver.find (/Users/robmaes/Documents/ccp/api/node_modules/mikro-orm/dist/drivers/AbstractSqlDriver.js:23:82)
    at EntityManager.find (/Users/robmaes/Documents/ccp/api/node_modules/mikro-orm/dist/EntityManager.js:76:43)
    at Collection.init (/Users/robmaes/Documents/ccp/api/node_modules/mikro-orm/dist/entity/Collection.js:116:32)
    at StoreController.findAll (/Users/robmaes/Documents/ccp/api/dist/store/store.controller.js:29:31)
    at /Users/robmaes/Documents/ccp/api/node_modules/@nestjs/core/router/router-execution-context.js:37:29
    at processTicksAndRejections (internal/process/task_queues.js:94:5)
    at async /Users/robmaes/Documents/ccp/api/node_modules/@nestjs/core/router/router-execution-context.js:45:28
    at async Object.<anonymous> (/Users/robmaes/Documents/ccp/api/node_modules/@nestjs/core/router/router-proxy.js:8:17)

@willsoto willsoto changed the title Composite join key with metadata example buggy or incomplete Composite join key with metadata example incomplete May 1, 2020
@B4nan B4nan closed this as completed in a4c56ed May 2, 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

3 participants