Note: This is a very complex application (I've chosen to build upon AdonisJS), therefore, I am going to share only pieces of code that are necessary so we get to solution fast.
I have built and deployed several apps with AdonisJS but this is the first time I've met this issue.
If a solution already exists to this exact issue, kindly advise.
When I call this relation via EagerLoading with, I get an error:
Error: this.belongsTo is not a function
Now, I have been through the documentation several times. It does say that the second parameter should be the primary key of the model when defining the belongsTo relation.
I find this an unnecessary constraint in my opinion, one should be able to choose what relation should form though what columns.
I'd like to make a request for the possibility of:
belongsTo(relatedModel, anyKey /* focus */, foreignKey)
instead of:
belongsTo(relatedModel, primaryKey /* focus */, foreignKey)
I also use Laravel for applications that require PHP, and it is possible there.
this.create('currencies', (table) => {
table.increments()
...
table.string('symbol')
table.string('name')
table.string('currency') // points to products.currency
...
})
```javascript
this.create('products', (table) => {
table.increments()
...
table.string('title')
table.string('description')
table.string('currency') // points to currencies.currency
...
})
#### Relations
```javascript
class Currency extends Model {
products () {
return this.hasMany('App/Models/Product', 'currency', 'currency')
// since both columns are named currency
}
}
```javascript
class Product extends Model {
currency () {
return this.belongsTo('App/Models/Currency', 'currency', 'currency')
}
}
#### Controller action
```javascript
class ProductController
{
async all ({ request })
{
return await Product.query().with('currency').fetch()
// I've simplified the query so that it we can focus on with('currency')
}
}
"@adonisjs/framework": "^5.0.9",
"@adonisjs/lucid": "^6.1.3",
hello? anybody?
I think the problem here is that Lucid ORM don't understand if it must join on the function "currency" or on the column "currency".
To fix this situation, I think the best solution is to rename the column like that for example:
// solution 1
class Product extends Model {
currency () {
return this.belongsTo('App/Models/Currency', 'currency_rel', 'currency')
}
}
// solution 2
class Product extends Model {
currencyRel () {
return this.belongsTo('App/Models/Currency', 'currency', 'currency')
}
}
Hey @rauschmerscen! 馃憢
Could you please create a repository with the minimum amount of code to reproduce your issue?
@verdonarthur Yes. your second solution renaming the relation did work.
I did not try the first since I cannot change column names in this project).
Thank you very much.
I must mention here that even when I created another renamed relation currencee() and stopped using currency(), it did not work until I had to completely remove currency(), and then it worked.
class Product extends Model {
// had to completely remove it
currency () {
return this.belongsTo('App/Models/Currency', 'currency', 'currency')
}
// renaming relation currency
currencee () {
return this.belongsTo('App/Models/Currency', 'currency', 'currency')
}
}
@RomainLanz Issue Reproduce Repo
I see that you are not following our convention, FK should be named xxx_id.
Please, can you try by following the convention and let me know if it works?
I cannot rename columns. Laravel doesn't follow this convention either.
Same issue here. I have several projects sharing the same DB so I must not change column names. I don't understand why this.belongsTo allows you to override the primary key if it will impose you a convention regardless.
I think it's most probably a Knex.js issue, I'm not sure as I've not used it myself. But if it isn't then removing this convention allowing to choose whatever column name would provide ease and a sense of freedom in many scenarios.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Most helpful comment
@verdonarthur Yes. your second solution renaming the relation did work.
I did not try the first since I cannot change column names in this project).
Thank you very much.
I must mention here that even when I created another renamed relation currencee() and stopped using currency(), it did not work until I had to completely remove currency(), and then it worked.
class Product extends Model { // had to completely remove it currency () { return this.belongsTo('App/Models/Currency', 'currency', 'currency') } // renaming relation currency currencee () { return this.belongsTo('App/Models/Currency', 'currency', 'currency') } }