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.

Joined tables overwrite columns with the same name

See original GitHub issue

Came upon this today and it’s causing me quite a headache, I haven’t found a good way of resolving this.

Okay, so take for instance that we create a simple OneToMany join:

  var posts = Knex('posts')
      .join('comments', 'comments.parent_id', '=', 'posts.id')
      .where({ 'posts.id': 1}).select()
      .then(function(result){
          res.send(result);
      });

The results come back as a flat tree. That makes sense, just like a regular SQL return (meaning that if you have 10 comments, you’ll have 10 arrays with data from each comment + a copy of post data); however, if the posts table contains the column “name” and the comments table contains the column “name”, the joined table (comments in this case) will overwrite the parent table (posts). Same goes for dates and a bunch of others.

For instance, if you just query for the post, you may get:

[ {
    "id": 1,
    "name": "My first post",
    "content": "Little johnny went to the woods...",
    "date": "1/29/2013",
    "category_id": 1
}]

When querying a single comment, you may get the following:

[{
    "id": 3
    "name": "AntJanus"
    "content": "Hi, this is my first comment",
    "date": "1/31/2013",
    "email": "whatever@whatever.com"
    "parent_id": "1"
}]

The resulting join will look like so:

[
    {
        "id": 3,
        "name": "AntJanus",
        "content": "Hi, this is my first comment",
        "date": "1/31/2013",
        "email": "whatever@whatever.com",
        "category_id": 1,
        "parent_id": 1
    },
    {
        "id": 4,
        "name": "Some Other",
        "content": "Hi, this is my second comment",
        "date": "2/1/2013",
        "email": "whatever@whatever.com",
        "category_id": 1,
        "parent_id": 1
    }
]

And it will completely overwrite original name and content of the post.

I haven’t found a way around it with SQL other than using a select statement that aliases each individual column (there’s no way to mass alias in SQL). However, on large and variable tables, this is not really an option.

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Reactions:2
  • Comments:32 (4 by maintainers)

github_iconTop GitHub Comments

29reactions
aakremcommented, Apr 24, 2016

.options({ nestTables: true} solve the issue as the result will be each table in a different object.

22reactions
mrbeskincommented, Nov 29, 2016

Hey, I’ve ended up here a couple times and thought I would post to let people know what I’ve found.

The .options({nestTables:true}) doesn’t work in Postgres (it’s a MySQL feature, I believe), but luckily you can get similar functionality using the to_json tools in Postgres.

This is outlined in an issue for node-postgres here:

Nesting Tables in Node Postgres

Hope this helps if anyone ends up here like I did.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Two columns with the same name, one overwriting on join
I have a problem where Ihave two tables and one table the entry is null where as on the other it contains information....
Read more >
How not to overwrite Same columns name when multiple ...
i have an sql statement that retrives information from multiple tables but some of them have same columns name.
Read more >
Joins overwrite column with the same field - Qlik Community
Solved: Two tables, the load/select statements as follows: LOAD event_id, data_id AS data_amne_id; SQL SELECT * FROM `innan_portning`.
Read more >
Query syntax | BigQuery - Google Cloud
The WITH clause hides any permanent tables with the same name for the duration of the query, unless you qualify the table name,...
Read more >
ALTER TABLE REPLACE COLUMNS - Amazon Athena
Note that even if you are replacing just a single column, the syntax must be ALTER TABLE table-name REPLACE COLUMNS , with columns...
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