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

Custom primary key field name + scope with include causes primary key field to be duplicated on every query #4127

Closed
ghost opened this issue Jul 16, 2015 · 0 comments

Comments

@ghost
Copy link

ghost commented Jul 16, 2015

I have a one-to-many relationship between rooms and meetings. The meetings have a custom field name for the primary key column. The code is here: https://gist.github.com/sheepcount/b7505eec4ef82e34ed95.

When querying multiple times using my scope nested, the primary key column gets duplicated every time. At some point, query length will exceed the maximum field number of the database, and the app has to be restarted.

The problem seems to be in model.js: validateIncludedElement() checks for if (include.attributes.indexOf(attr) === -1), however, with custom field names, include.attributes will contain arrays for the fields and thus the check won't detect the primary key already present and re-add it every time a query is performed.

First query:

SELECT "Room"."id", "Room"."number", "Room"."createdAt", "Room"."updatedAt", 
 "Meetings"."customID" AS "Meetings.id", 
 "Meetings"."description" AS "Meetings.description", 
 "Meetings"."createdAt" AS "Meetings.createdAt", 
 "Meetings"."updatedAt" AS "Meetings.updatedAt", 
 "Meetings"."RoomId" AS "Meetings.RoomId" 
 FROM "Rooms" AS "Room" LEFT OUTER JOIN "Meetings" AS "Meetings" 
 ON "Room"."id" = "Meetings"."RoomId" WHERE "Room"."number" = '101';

Second query:

SELECT "Room"."id", "Room"."number", "Room"."createdAt", "Room"."updatedAt", 
 "Meetings"."customID" AS "Meetings.id", 
 "Meetings"."customID" AS "Meetings.id", 
 "Meetings"."description" AS "Meetings.description", 
 "Meetings"."createdAt" AS "Meetings.createdAt", 
 "Meetings"."updatedAt" AS "Meetings.updatedAt", 
 "Meetings"."RoomId" AS "Meetings.RoomId" 
 FROM "Rooms" AS "Room" LEFT OUTER JOIN "Meetings" AS "Meetings" 
 ON "Room"."id" = "Meetings"."RoomId" WHERE "Room"."number" = '101';

Third query:

SELECT "Room"."id", "Room"."number", "Room"."createdAt", "Room"."updatedAt", 
 "Meetings"."customID" AS "Meetings.id", 
 "Meetings"."customID" AS "Meetings.id", 
 "Meetings"."customID" AS "Meetings.id", 
 "Meetings"."description" AS "Meetings.description", 
 "Meetings"."createdAt" AS "Meetings.createdAt", 
 "Meetings"."updatedAt" AS "Meetings.updatedAt", 
 "Meetings"."RoomId" AS "Meetings.RoomId" 
 FROM "Rooms" AS "Room" LEFT OUTER JOIN "Meetings" AS "Meetings" 
 ON "Room"."id" = "Meetings"."RoomId" WHERE "Room"."number" = '101';
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

1 participant