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
I'm working on a project using Adonis/Lucid and I'm facing an issue with managing user profiles. I have a users table with different user types, and each user type should have a unique profile. I want to simplify the relationship by having a single Profile relation in the User model that dynamically selects the appropriate profile based on the user's type.
Tables and Models
User Model
import { BaseModel, column, hasOne } from '@adonisjs/lucid/orm';
import ProfileBeneficiary from './profile_beneficiary.js';
import ProfileProvider from './profile_provider.js';
import ProfileStaff from './profile_staff.js';
export default class User extends BaseModel {
@column()
declare type: number;
@hasOne(() => ProfileBeneficiary)
declare profileBeneficiary: HasOne<typeof ProfileBeneficiary>;
@hasOne(() => ProfileStaff)
declare profileStaff: HasOne<typeof ProfileStaff>;
@hasOne(() => ProfileProvider)
declare profileProvider: HasOne<typeof ProfileProvider>;
}
Profile Model
import { BaseModel, belongsTo, column } from '@adonisjs/lucid/orm';
import User from './user.js';
export default class Profile extends BaseModel {
@column()
declare userId: string;
@belongsTo(() => User)
declare user: BelongsTo<typeof User>;
}
Beneficiary Profile Model
import Profile from './profile.js';
import { belongsTo } from '@adonisjs/lucid/orm';
import Address from './address.js';
export default class ProfileBeneficiary extends Profile {
@belongsTo(() => Address)
declare address: BelongsTo<typeof Address>;
}
Provider Profile Model
import Profile from './profile.js';
import { column, hasOne } from '@adonisjs/lucid/orm';
import Address from './address.js';
export default class ProfileProvider extends Profile {
@hasOne(() => Address)
declare companyAddress: HasOne<typeof Address>;
}
Staff Profile Model
import Profile from './profile.js';
import { belongsTo, hasManyThrough } from '@adonisjs/lucid/orm';
import ProfileBeneficiary from './profile_beneficiary.js';
import Organism from './organism.js';
export default class ProfileStaff extends Profile {
@belongsTo(() => Organism)
declare organism: BelongsTo<typeof Organism>;
@hasManyThrough(() => ProfileBeneficiary, {
foreignKey: 'staffId',
})
declare assigned: HasManyThrough<typeof ProfileBeneficiary>;
}
Desired Change
I want to simplify the User model by removing the separate profile relations (profileBeneficiary, profileStaff, profileProvider) and replace them with a single Profile relation. This Profile should dynamically select the appropriate profile based on the user's type.
Current Database Schema for User
this.schema.createTable(this.tableName, (table) => {
table.uuid('id').primary();
table.integer('type').notNullable();
// Other fields...
});
Additional Information
There is no profile table, as the profiles extend the Profile model directly.
Each profile has its own set of attributes and relationships.
Questions
How can I modify the User model to achieve this dynamic profile selection using Adonis/Lucid?
Do I need to make changes to the existing Profile models?
Can you provide an example or guidance on how to implement this?
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
-
I'm working on a project using Adonis/Lucid and I'm facing an issue with managing user profiles. I have a users table with different user types, and each user type should have a unique profile. I want to simplify the relationship by having a single Profile relation in the User model that dynamically selects the appropriate profile based on the user's type.
Tables and Models
User Model
Profile Model
Beneficiary Profile Model
Provider Profile Model
Staff Profile Model
Desired Change
I want to simplify the User model by removing the separate profile relations (profileBeneficiary, profileStaff, profileProvider) and replace them with a single Profile relation. This Profile should dynamically select the appropriate profile based on the user's type.
Current Database Schema for User
Additional Information
There is no profile table, as the profiles extend the Profile model directly.
Each profile has its own set of attributes and relationships.
Questions
Thank you for your assistance!
Beta Was this translation helpful? Give feedback.
All reactions