From efd0fcd6848a0313374fd6061a7ca6fc4ca3f0db Mon Sep 17 00:00:00 2001 From: Alec Hirsch Date: Mon, 26 Mar 2018 17:24:39 -0400 Subject: [PATCH 1/2] fixed issue where relation query returns thousands of duplicate rows --- src/relation.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/relation.js b/src/relation.js index 3d8e78bd..b775caa8 100644 --- a/src/relation.js +++ b/src/relation.js @@ -233,7 +233,7 @@ export default RelationBase.extend({ const currentColumns = _.find(knex._statements, {grouping: 'columns'}); if (!currentColumns || currentColumns.length === 0) { - knex.column(this.targetTableName + '.*'); + knex.distinct(this.targetTableName + '.*'); } if (this.isJoined()) this.joinColumns(knex); From c88db3a59100231ffcc6ab294edef85baafd3694 Mon Sep 17 00:00:00 2001 From: Alec Hirsch Date: Tue, 27 Mar 2018 08:58:51 -0400 Subject: [PATCH 2/2] fixed tests --- test/integration/relation.js | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/test/integration/relation.js b/test/integration/relation.js index 9ffb68ef..9551c88e 100644 --- a/test/integration/relation.js +++ b/test/integration/relation.js @@ -158,7 +158,7 @@ module.exports = function(Bookshelf) { // init the select constraints relatedData.selectConstraints(_knex, {}); - equal(_knex.toString(), 'select `doctormeta`.* from `doctormeta` where `doctormeta`.`doctoring_id` = 1 limit 1'); + equal(_knex.toString(), 'select distinct `doctormeta`.* from `doctormeta` where `doctormeta`.`doctoring_id` = 1 limit 1'); }); it('should handle a hasOne -> through relation', function() { @@ -193,7 +193,7 @@ module.exports = function(Bookshelf) { // init the select constraints relatedData.selectConstraints(_knex, {}); - var sql = 'select `account_histories`.*, `accounts`.`id` as `_pivot_id`, `accounts`.`supplier_id` as `_pivot_supplier_id` from `account_histories` inner join `accounts` on `accounts`.`id` = `account_histories`.`account_id` where `accounts`.`supplier_id` = 1 limit 1'; + var sql = 'select distinct `account_histories`.*, `accounts`.`id` as `_pivot_id`, `accounts`.`supplier_id` as `_pivot_supplier_id` from `account_histories` inner join `accounts` on `accounts`.`id` = `account_histories`.`account_id` where `accounts`.`supplier_id` = 1 limit 1'; equal(_knex.toString(), sql); }); @@ -230,7 +230,7 @@ module.exports = function(Bookshelf) { // init the select constraints relatedData.selectConstraints(_knex, {}); - var sql = 'select `suppliers`.*, `accounts`.`id` as `_pivot_id`, `accounts`.`supplier_id` as `_pivot_supplier_id` from `suppliers` inner join `accounts` on `accounts`.`supplier_id` = `suppliers`.`id` inner join `account_histories` on `accounts`.`id` = `account_histories`.`account_id` where `account_histories`.`id` = 1 limit 1'; + var sql = 'select distinct `suppliers`.*, `accounts`.`id` as `_pivot_id`, `accounts`.`supplier_id` as `_pivot_supplier_id` from `suppliers` inner join `accounts` on `accounts`.`supplier_id` = `suppliers`.`id` inner join `account_histories` on `accounts`.`id` = `account_histories`.`account_id` where `account_histories`.`id` = 1 limit 1'; equal(_knex.toString(), sql); }); @@ -267,7 +267,7 @@ module.exports = function(Bookshelf) { // init the select constraints relatedData.selectConstraints(_knex, {}); - var sql = 'select `patients`.*, `appointments`.`id` as `_pivot_id`, `appointments`.`doctor_id` as `_pivot_doctor_id`, `appointments`.`patient_id` as `_pivot_patient_id` from `patients` inner join `appointments` on `appointments`.`patient_id` = `patients`.`id` where `appointments`.`doctor_id` = 1'; + var sql = 'select distinct `patients`.*, `appointments`.`id` as `_pivot_id`, `appointments`.`doctor_id` as `_pivot_doctor_id`, `appointments`.`patient_id` as `_pivot_patient_id` from `patients` inner join `appointments` on `appointments`.`patient_id` = `patients`.`id` where `appointments`.`doctor_id` = 1'; equal(_knex.toString(), sql); }); @@ -297,7 +297,7 @@ module.exports = function(Bookshelf) { // init the select constraints relatedData.selectConstraints(_knex, {}); - var sql = 'select `patients`.*, `doctors_patients`.`doctor_id` as `_pivot_doctor_id`, `doctors_patients`.`patient_id` as `_pivot_patient_id` from `patients` inner join `doctors_patients` on `doctors_patients`.`patient_id` = `patients`.`id` where `doctors_patients`.`doctor_id` = 1'; + var sql = 'select distinct `patients`.*, `doctors_patients`.`doctor_id` as `_pivot_doctor_id`, `doctors_patients`.`patient_id` as `_pivot_patient_id` from `patients` inner join `doctors_patients` on `doctors_patients`.`patient_id` = `patients`.`id` where `doctors_patients`.`doctor_id` = 1'; equal(_knex.toString(), sql); }); @@ -325,7 +325,7 @@ module.exports = function(Bookshelf) { // init the select constraints relatedData.selectConstraints(_knex, {}); - var sql = "select `photos`.* from `photos` where `photos`.`imageable_id` = 1 and `photos`.`imageable_type` = 'doctors'"; + var sql = "select distinct `photos`.* from `photos` where `photos`.`imageable_id` = 1 and `photos`.`imageable_type` = 'doctors'"; equal(_knex.toString(), sql); }); @@ -353,7 +353,7 @@ module.exports = function(Bookshelf) { // init the select constraints relatedData.selectConstraints(_knex, {}); - equal(_knex.toString(), 'select `translations`.* from `translations` where `translations`.`code` = \'en\' limit 1'); + equal(_knex.toString(), 'select distinct `translations`.* from `translations` where `translations`.`code` = \'en\' limit 1'); }); it('should handle a hasOne -> through relation with explicit foreignKeyTarget', function() { @@ -388,7 +388,7 @@ module.exports = function(Bookshelf) { // init the select constraints relatedData.selectConstraints(_knex, {}); - equal(_knex.toString(), 'select `locales`.*, `translations`.`code` as `_pivot_code`, `translations`.`customer` as `_pivot_customer` from `locales` inner join `translations` on `translations`.`code` = `locales`.`isoCode` where `translations`.`customer` = \'foobar\' limit 1'); + equal(_knex.toString(), 'select distinct `locales`.*, `translations`.`code` as `_pivot_code`, `translations`.`customer` as `_pivot_customer` from `locales` inner join `translations` on `translations`.`code` = `locales`.`isoCode` where `translations`.`customer` = \'foobar\' limit 1'); }); it('should handle a hasMany relation with explicit foreignKeyTarget', function() { @@ -414,7 +414,7 @@ module.exports = function(Bookshelf) { // init the select constraints relatedData.selectConstraints(_knex, {}); - equal(_knex.toString(), 'select `translations`.* from `translations` where `translations`.`code` = \'en\''); + equal(_knex.toString(), 'select distinct `translations`.* from `translations` where `translations`.`code` = \'en\''); }); it('should handle a hasMany -> through relation with explicit foreignKeyTarget', function() { @@ -449,7 +449,7 @@ module.exports = function(Bookshelf) { // init the select constraints relatedData.selectConstraints(_knex, {}); - equal(_knex.toString(), 'select `locales`.*, `translations`.`code` as `_pivot_code`, `translations`.`customer` as `_pivot_customer` from `locales` inner join `translations` on `translations`.`code` = `locales`.`isoCode` where `translations`.`customer` = \'foobar\''); + equal(_knex.toString(), 'select distinct `locales`.*, `translations`.`code` as `_pivot_code`, `translations`.`customer` as `_pivot_customer` from `locales` inner join `translations` on `translations`.`code` = `locales`.`isoCode` where `translations`.`customer` = \'foobar\''); }); it('should handle a belongsTo relation with explicit foreignKeyTarget', function() { @@ -475,7 +475,7 @@ module.exports = function(Bookshelf) { // init the select constraints relatedData.selectConstraints(_knex, {}); - var sql = 'select `locales`.* from `locales` where `locales`.`isoCode` = \'en\' limit 1'; + var sql = 'select distinct `locales`.* from `locales` where `locales`.`isoCode` = \'en\' limit 1'; equal(_knex.toString(), sql); }); @@ -512,7 +512,7 @@ module.exports = function(Bookshelf) { // init the select constraints relatedData.selectConstraints(_knex, {}); - var sql = 'select `customers`.*, `translations`.`code` as `_pivot_code`, `translations`.`customer` as `_pivot_customer` from `customers` inner join `translations` on `translations`.`customer` = `customers`.`name` inner join `locales` on `translations`.`code` = `locales`.`isoCode` where `locales`.`isoCode` = \'en\' limit 1'; + var sql = 'select distinct `customers`.*, `translations`.`code` as `_pivot_code`, `translations`.`customer` as `_pivot_customer` from `customers` inner join `translations` on `translations`.`customer` = `customers`.`name` inner join `locales` on `translations`.`code` = `locales`.`isoCode` where `locales`.`isoCode` = \'en\' limit 1'; equal(_knex.toString(), sql); }); @@ -543,7 +543,7 @@ module.exports = function(Bookshelf) { // init the select constraints relatedData.selectConstraints(_knex, {}); - var sql = 'select `customers`.*, `translations`.`code` as `_pivot_code`, `translations`.`customer` as `_pivot_customer` from `customers` inner join `translations` on `translations`.`customer` = `customers`.`name` where `translations`.`code` = \'en\''; + var sql = 'select distinct `customers`.*, `translations`.`code` as `_pivot_code`, `translations`.`customer` as `_pivot_customer` from `customers` inner join `translations` on `translations`.`customer` = `customers`.`name` where `translations`.`code` = \'en\''; equal(_knex.toString(), sql); }); @@ -581,7 +581,7 @@ module.exports = function(Bookshelf) { // init the select constraints relatedData.selectConstraints(_knex, {}); - var sql = 'select `customers`.*, `translations`.`code` as `_pivot_code`, `translations`.`code` as `_pivot_code`, `translations`.`customer` as `_pivot_customer` from `customers` inner join `translations` on `translations`.`customer` = `customers`.`name` where `translations`.`code` = \'en\''; + var sql = 'select distinct `customers`.*, `translations`.`code` as `_pivot_code`, `translations`.`code` as `_pivot_code`, `translations`.`customer` as `_pivot_customer` from `customers` inner join `translations` on `translations`.`customer` = `customers`.`name` where `translations`.`code` = \'en\''; equal(_knex.toString(), sql); });