Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fetchAll on query func returns duplicated model objects #1763

Closed
godievski opened this issue Feb 16, 2018 · 2 comments
Closed

fetchAll on query func returns duplicated model objects #1763

godievski opened this issue Feb 16, 2018 · 2 comments

Comments

@godievski
Copy link

godievski commented Feb 16, 2018

Introduction

As the title says, when I use the query function over a model and then use fetchAll, I got duplicated objects (bookshelf models).

Issue Description

I have two tables: (table_a & table_b)

**table_a**:
id (PK) - column_1 - column_2 - column_fk (FK)
**table_b**:
column_fk (PK), column_fk2(PK), column_3, column_4

sql:
select table_a.id, table_a.column_1, table_b.column.fk, table_b.column_3 from table_a inner join table_b on table_a.column_fk = table_b.column_fk;

On bookshelf:
model for table_a:

model_a = bookshelf.Model.extend({ tableName: 'table_a'},
{
  my_query: function(){
    return this.query(function(qb){
       qb.innerJoin('table_b', 'table_b.column_fk', 'table_a.column_fk');
       qb.select(['table_a.id', 'table_a.column_1']);
       qb.select(['table_b.column_fk', table_b.column_3']);
    }).fetchAll();
  }
});

Packages version

database: postgresql
knex: v0.13.0
bookshelf: v.0.12.1
nodejs: 6.12.3

Expected behaviour

I should get probably two or more rows/objects that have the same id but with the other parameters completly different (assuming this on the database).
I tried this sql query directly on my database and I got what I supposed to get.

example:

[ {id: 'uuid_1', column_1: 'foo', column_fk: 'uuid_foo_1', column_3: 'largeeee_foooooo'},
  {id: 'uuid_1', column_1: 'foo', column_fk: 'uuid_foo_1', column_3: 'short_foo'}, ...
]

Actual behaviour

I got duplicated objects that have the same id

[ {id: 'uuid_1', column_1: 'foo', column_fk: 'uuid_foo_1', column_3: 'largeeee_foooooo'},
  {id: 'uuid_1', column_1: 'foo', column_fk: 'uuid_foo_1', column_3: 'largeeee_foooooo'}, ...
]

PD: I checked what knex is returning and it's the same what I got when querying directly on my database, meaning no bugs (so this is no knex's issue)
PD2: This issue can be solved ussing qb.select('table_a.id as some_id'). However, I think this should be fixed.

@ricardograca
Copy link
Member

Could be related to #1554, and not specifically about using .query() with fetchAll().

@ricardograca
Copy link
Member

Fixed in #1846.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants