Closed jordykoppen closed 6 years ago
I tried the exact code and it works fine. Is there something else that you didn’t shared? Also can you please share the complete error stack?
Thanks for the fast reply. Tomorrow I'll post an update when I'm back at it
Alright, so I did a check and still have the same issue. Let me post all of the related code:
User Schema
this.create('users', table => {
table.increments()
table.string('name', 254).notNullable()
table.string('email', 254).notNullable().unique()
table.integer('role').unsigned()
table.string('password', 60).notNullable()
table.timestamps()
})
Role Schema
this.create('roles', (table) => {
table.increments()
table.string('name', 254).notNullable()
table.string('label', 254).notNullable()
table.timestamps()
})
After, I have a migration that will assign all foreign keys:
this.table('users', (table) => {
table.foreign('role').references('roles.id').onDelete('SET NULL')
})
With my migrations covered, the User Model
role () {
return this.hasOne('App/Models/Role', 'role', 'id')
}
And finally the function inside UserController I call trough an endpoint
async getUser ({ response, auth }) {
try {
const user = await auth.getUser()
await user.role().fetch()
return user
} catch (err) {
response.send('Missing or invalid JWT token')
}
}
Where can I find the complete error stack? The only thing it refers to is the line where this.hasOne is called..
Would you mind sharing a repo with the minimum required code to reproduce this issue?
No worries, will do once I'm home
Made a repo with instructions in the README.md
https://github.com/jordykoppen/adonis-940-replica
Your data attributes has a field called role
, which is conflicting with the role
property on the model.
Ideally the field name inside the database must be role_id
.
But yes, I can improve the error message in this case
Alright, makes sense! Being on this topic, is there any way to
Like:
const user = await User.find(1)
await user.role().fetch()
// Returns
{
name: 'John',
role: {
id: 1,
name: 'admin'
},
...
}
I only could find an example in the docs about using query().with().fetch()
which would then return all the users?
Yup, you can read about it here https://adonisjs.com/docs/4.1/relationships#_lazy_eager_loading.
This Eagerloads for that user and attaches it to the user instance.
await user.load('role')
return user.toJSON()
Oops! completely missed that.. :)
Thanks for your time and help 👍
As a suggestion it would be nice to have that tip in the documentation, it may be common to have a column on the DB with the same name as the relationship on the model.
A curious thing, this problem only occurred on my staging server but worked just fine in my local machine.
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
I have an issue with one of my models and using a relation.
User Model:
On my user schema I have a foreign key called
role
When I try and call this:
I get the error returned
this.hasOne is not a function
Can somebody help me figure out what I'm doing wrong here?