Replies: 8 comments
-
The client belongs to an organization in this case. The simplest trick to differentiate between |
Beta Was this translation helpful? Give feedback.
-
Thank you very much for your quick response. I made the change you mentioned, but I am getting an error.
model/client.ts export default class Client extends compose(BaseModel, AuthFinder) {
@column({ isPrimary: true })
declare clientId: string;
@belongsTo(() => Organization, {
foreignKey: 'organizationId',
})
declare organizationId: BelongsTo<typeof Organization>;
} Could I have used it incorrectly? const dataNewOrg = {
organizationId: randomUUID(),
};
const newOrganization = await Organization.create(dataNewOrg);
await client.related('organizationId').associate(newOrganization); |
Beta Was this translation helpful? Give feedback.
-
Well, I'm definitely doing something wrong, and I don't understand what. I'm using the model in another function where I need organization, and when I load it, it gives me the same error. public async details({ response, auth }: HttpContext) {
const client = auth.getUserOrFail(); // I'm using the Adonis authentication module.
await client.load('organizationId');
} Thanks! |
Beta Was this translation helpful? Give feedback.
-
I am not sure why you are defining the relationship on the foreign key. Maybe Mikro ORM has this thing. Lemme share how it should be setup.
class Client extends BaseModel {
@column()
clientId: string
@column()
organizationId: string
@column.dateTime()
createdAt: DateTime
@column.dateTime()
updatedAt: DateTime
}
class Organization extends BaseModel {
@column()
organizationId: string
@column.dateTime()
createdAt: DateTime
@column.dateTime()
updatedAt: DateTime
} Till now, you have one to one mapping between the tables and the models. Now, you define a relationship as follows. class Client extends BaseModel {
@column()
clientId: string
@column()
organizationId: string
@column.dateTime()
createdAt: DateTime
@column.dateTime()
updatedAt: DateTime
+ @belongsTo(() => Organization)
+ organization: BelongsTo<typeof Organization>
} |
Beta Was this translation helpful? Give feedback.
-
Hello! Thank you very much for your response. Indeed, I was confused with MikroORM; it does map the relationship at the database level as well as in ORM using the same field. I made the changes you mentioned, and now I'm getting another error:
I found on GitHub that it might be due to the primary key not being defined, but it is. It might not be related, but it's the only thing I found. Here’s the code as it stands: models/organization.ts export default class Organization extends BaseModel {
@column({ isPrimary: true })
declare organizationId: string;
@column()
declare name: string;
@column.dateTime({ autoCreate: true })
declare createdAt: DateTime;
@column.dateTime({ autoCreate: true, autoUpdate: true })
declare updatedAt: DateTime;
} models/client.ts export default class Client extends compose(BaseModel, AuthFinder) {
@column({ isPrimary: true })
declare clientId: string;
@column()
declare organizationId: string;
@column.dateTime({ autoCreate: true })
declare createdAt: DateTime;
@column.dateTime({ autoCreate: true, autoUpdate: true })
declare updatedAt: DateTime;
@belongsTo(() => Organization, {
foreignKey: 'organizationId',
})
declare organization: BelongsTo<typeof Organization>;
} controllers/organizations.ts const newOrganization = await Organization.create({
organizationId: randomUUID(),
});
await client.related('organization').associate(newOrganization); // line 67 Thanks again, and sorry for so many questions. |
Beta Was this translation helpful? Give feedback.
-
Hello! I'm still testing and experimenting. It seems that if I set Thanks! model/client.ts export default class Client extends compose(BaseModel, AuthFinder) {
static selfAssignPrimaryKey = true;
@column({ isPrimary: true })
declare clientId: string;
@column()
declare organizationId: string;
@beforeCreate()
static assignUuid(client: Client) {
client.clientId = randomUUID();
}
@belongsTo(() => Organization, {
foreignKey: 'organizationId',
})
declare organization: BelongsTo<typeof Organization>;
} model/organization.ts export default class Organization extends BaseModel {
static selfAssignPrimaryKey = true;
@column({ isPrimary: true })
declare organizationId: string;
@column()
declare name: string;
@column.dateTime({ autoCreate: true })
declare createdAt: DateTime;
@column.dateTime({ autoCreate: true, autoUpdate: true })
declare updatedAt: DateTime;
@beforeCreate()
static assignUuid(organization: Organization) {
organization.organizationId = randomUUID();
}
} controller/organization.ts const newOrganization = await Organization.create({
name,
});
client.organizationId = newOrganization.organizationId;
await client.save(); |
Beta Was this translation helpful? Give feedback.
-
Okay, so let me share more info around the primary keys this time
Since you have set the const newOrganization = await Organization.create({
name,
organizationId: randomUUID(),
}); |
Beta Was this translation helpful? Give feedback.
-
Hi! Thank you very much for your help, I think I've understood everything now. I already have the project running without any issues! |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi, I'm working on a development project and using Lucid, but I don’t fully understand how relationships work. I come from Mikro ORM and maybe I’m getting confused.
I have two tables, Clients and Organizations, where a client has an organization.
migration/client.ts
migration/organization.ts
model/client.ts
model/organization.ts
My idea is that when I create the organization, it should update the field in the Clients table.
I thought that by doing this, the UUID of the organization would be automatically assigned in the table, but it’s not working.
The organization is created perfectly, but the organization_id field in the Clients table remains null.
clients table

Clearly, I am confusing something and doing it wrong. What should I do?
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions