Update model instance with loaded aggregate columns #4174
-
Hello there! 👋 I want to update model instance like this export default class User extends BaseModel {
@column({ isPrimary: true })
public id: number
@column()
public login: string
@column({ serializeAs: null })
public password: string
@column.dateTime({ autoCreate: true })
public createdAt: DateTime
@column.dateTime({ autoCreate: true, autoUpdate: true })
public updatedAt: DateTime
// Here is an aggregate column
@column({ columnName: 'profiles_count' })
public profilesCount: number
@hasMany(() => Profile)
public profiles: HasMany<typeof Profile>
} which loaded aggregate column like this await item.loadCount('profiles') but it throws error like this column "profiles_count" of relation "users" does not exist I update model instance like this // item is a user model instance
await item.merge(modelData).save() How can i resolve this problem? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
Hello @DZ8540 I have try the const user = await User.query()
.where("id", auth.user!.id)
.firstOrFail();
await user.loadCount('profile');
console.log(user.$extras)
// { profile_count: 1 } And here is my model: export default class User extends BaseModel {
@column({ isPrimary: true })
public id: number;
@column()
public name: string;
@hasOne(() => Profile)
public profile: HasOne<typeof Profile>;
@column()
public email: string;
@column({ serializeAs: null })
public password: string;
@column()
public rememberMeToken: string | null;
@column.dateTime({ autoCreate: true })
public createdAt: DateTime;
@column.dateTime({ autoCreate: true, autoUpdate: true })
public updatedAt: DateTime;
@beforeSave()
public static async hashPassword(user: User) {
if (user.$dirty.password) {
user.password = await Hash.make(user.password);
}
}
} When I I hope my comment can help you, and if it's not true can you provide me more context ? |
Beta Was this translation helpful? Give feedback.
-
Hello, |
Beta Was this translation helpful? Give feedback.
It should be a computed property. Checks docs for model computed properties