question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Setting a relationship's foreign key as tableName_primaryKey doesn't work with snakecased names

See original GitHub issue

According to the docs http://adonisjs.com/docs/4.0/relationships#_has_many

Quick example

cars() {
    return this.hasMany('App/Models/Cars', 'id', 'user_cars_user_id')
}

This relation is going to look into user table because the string is split by underscores, hence you can’t target tables with underscores in its name. Same happens with foreign key columns.

Because of that, the delimiter must be a dot: 'user_cars.user_id' (in the form tableName.fieldName), right?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
thetutlagecommented, Mar 29, 2018

Maybe you didn’t read it properly. As per the docs

Defaults to tableName_primaryKey of the current model. The singular form of the table name is used.

The convention to use the foreignKey is based on the tableName_primaryKey. However, you can define your own custom foreignKey.

But it doesn’t say that your foreignKey will be parsed to pull the tablename and then primaryKey.

It’s not “everywhere you make use of it”, its in the relationship method, where the docs explicitly say that you CAN provide said naming convention for the tableName_primaryKey.

Why the hell on the planet a Model will use a different table using Relationship.

0reactions
thetutlagecommented, Mar 29, 2018

Also when you think it’s a confusion on docs or implementation. Then you must ask in a clarification tone, instead of declaring everything as a bug.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Setting a relationship's foreign key as tableName_primaryKey ...
This relation is going to look into user table because the string is split by underscores, hence you can't target tables with underscores...
Read more >
3 common foreign key mistakes (and how to avoid them)
Foreign key constraints are important to any SQL database, but they can also cause problems if they're not implemented correctly.
Read more >
Can't set foreign key relationship - mysql workbench
The thing is when I enter a FK and choose a reference table I can't pick a referenced column. I can't click the...
Read more >
Create Foreign Key Relationships - SQL Server | Microsoft Learn
This article describes how to create foreign key relationships in SQL Server by using SQL Server Management Studio or Transact-SQL.
Read more >
Running a relationship analysis) - IBM
A relationship analysis identifies pairs of foreign keys and primary keys. Use this information to determine how data assets can be joined. Run...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found