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

1:1 relation with "eager: true" not showing up as reference #17

Open
mbise1993 opened this issue Jan 25, 2023 · 2 comments
Open

1:1 relation with "eager: true" not showing up as reference #17

mbise1993 opened this issue Jan 25, 2023 · 2 comments

Comments

@mbise1993
Copy link

Description
I have a couple of entities defined which have a 1:1 relation:

@Entity()
export class User extends BaseEntity<User, 'id'> {
  @PrimaryKey()
  id!: number;

  @Property()
  @Index()
  name!: string;

  @OneToOne(() => NotificationPreferences, (preferences) => preferences.user, {
    ref: true,
    eager: true,
    orphanRemoval: true,
  })
  notificationPreferences!: Ref<NotificationPreferences>;
}

@Entity()
export class NotificationPreferences extends BaseEntity<
  NotificationPreferences,
  'id'
> {
  @PrimaryKey()
  id!: number;

  @Property()
  frequency!: string;

  @OneToOne(() => User, (user) => user.notificationPreferences, {
    ref: true,
    owner: true,
  })
  user!: Ref<User>;
}

When I navigate to the "User" resource in AdminJS and select a user, the "Notification Preferences" field is blank. However, if I navigate to the "Notification Preferences" resource, I can see the linked user. After some experimenting, I've found that I can get it to work by removing the eager: true option from the notificationPreferences reference on the User entity.

Installed libraries
"@adminjs/express": "^5.0.1",
"@adminjs/mikroorm": "^2.0.0",
"@adminjs/nestjs": "^5.1.0",
"@mikro-orm/core": "^5.6.0",
"@mikro-orm/nestjs": "^5.1.2",
"@mikro-orm/postgresql": "^5.6.0",

Additional details
Here is the JSON response from the request to /admin/api/resources/User/records/1/show with the eager: true option set:

{
  "params": {
    "id": 1,
    "name": "Admin Use",
    "notificationPreferences.id": 1,
    "notificationPreferences.frequency": "every",
    "notificationPreferences.lastEmailDate": null,
    "notificationPreferences.user": 1
  },
  "populated": {
    "musician": { ...data }
  },
  "baseError": null,
  "errors": {},
  "id": 1,
  "title": "Admin Use",
  "recordActions": [ ...data ],
  "bulkActions": []
}

And without the eager: true option set (this works):

{
  "params": {
    "id": 1,
    "name": "Admin Use",
    "notificationPreferences": 1
  },
  "populated": {
    "notificationPreferences": { ...data },
    "musician": { ...data }
  },
  "baseError": null,
  "errors": {},
  "id": 1,
  "title": "Admin Use",
  "recordActions": [ ...data ],
  "bulkActions": []
}
@dziraf
Copy link
Collaborator

dziraf commented Mar 15, 2023

This might work if you define a @Property for notificationPreferencesId

@mbise1993
Copy link
Author

My relation is going the other way actually, there's a userId column on NotificationPreferences, but no notificationPreferencesId column on User. I did try defining a @Property for userId, but no luck

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