You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import{NormalizeConstructor}from'@ioc:Adonis/Core/Helpers'importtype*asOrmfrom'@ioc:Adonis/Lucid/Orm'import{DatabaseNotificationasDatabaseNotificationContract}from'@ioc:Adonis/Addons/Notifications'/** * @NOTICE Exporting a function instead of the class itslef allows us to use the decorators inside the mixin class. */exportdefaultfunction({ hasMany }: typeofOrm,DatabaseNotification: DatabaseNotificationContract){returnfunctionHasDatabaseNotifications<TextendsNormalizeConstructor<Orm.LucidModel>>(Superclass: T){class_HasDatabaseNotificationsextendsSuperclass{
@hasMany(()=>DatabaseNotification)publicnotifications: Orm.HasMany<typeofDatabaseNotification>asyncreadNotifications(){constself=thisasOrm.LucidRowreturnawaitself.load('notifications',(query)=>{query.whereNotNull('read_at')})}asyncunreadNotifications(){constself=thisasOrm.LucidRowreturnawaitself.load('notifications',(query)=>{query.whereNull('read_at')})}}return_HasDatabaseNotifications}}
Now I want to run some tests, so I created a mixin.spec.ts file and setup the application with database and providers. I have created a little function to generate my user model for test:
Now comes the problem, when I want to run the test, I get the following error: E_MISSING_MODEL_ATTRIBUTE: "User.notifications" expects "id" to exist on "User" model, but is missing
As you can see, the User model has the id attribute but the error says that it doesn't. It's weird, right?
If I remove the hasMany relationship everything works fine.
If I add the id attribute to the mixin HasDatabaseNotifications, it also works... but I don't want that, the id must be used in the final model, not in the top level model because if someone wants to use uuid or whatever else instead of simple numeric id, I won't force him...
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
@adonis/core 5.4.2
@adonisjs/lucid 16.3.2
Hello!
I'm facing a strange problem and I hope you could help me.
I'm trying to create a package for Adonis, it consists of a set of mixins that can be inherited by models.
I apologize because it will be a little long.
I have the main mixin file called Notifiable:
The above mixin extends from 2 other mixins:
The
HasDatabaseNotifications
mixin defines ahasMany
relation with the DatabaseNotification model:I register all of that within this provider:
Now I want to run some tests, so I created a
mixin.spec.ts
file and setup the application with database and providers. I have created a little function to generate my user model for test:Now comes the problem, when I want to run the test, I get the following error:
E_MISSING_MODEL_ATTRIBUTE: "User.notifications" expects "id" to exist on "User" model, but is missing
As you can see, the User model has the
id
attribute but the error says that it doesn't. It's weird, right?If I remove the
hasMany
relationship everything works fine.If I add the
id
attribute to the mixin HasDatabaseNotifications, it also works... but I don't want that, the id must be used in the final model, not in the top level model because if someone wants to use uuid or whatever else instead of simple numeric id, I won't force him...Can you help me, please?
Beta Was this translation helpful? Give feedback.
All reactions