Skip to content

Instantly share code, notes, and snippets.

@levity
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save levity/02052edd445b68174c1e to your computer and use it in GitHub Desktop.
Save levity/02052edd445b68174c1e to your computer and use it in GitHub Desktop.
module.exports = {
attributes: {
name: 'string',
slug: 'string',
users: {
collection: 'user',
via: 'communities',
through: 'communityuser'
}
},
autoCreatedAt: false,
autoUpdatedAt: false
};
module.exports = {
tableName: 'users_community',
tables: ['communities', 'users'],
junctionTable: true,
attributes: {
name: 'string',
email: 'string',
users: {
columnName: 'users_id',
type: 'integer',
foreignKey: true,
references: 'user',
on: 'id',
via: 'communities',
groupBy: 'user'
},
communities: {
columnName: 'community_id',
type: 'integer',
foreignKey: true,
references: 'community',
on: 'id',
via: 'users',
groupBy: 'community'
}
},
autoCreatedAt: false,
autoUpdatedAt: false
};
-- this is what happens when i load /community/9/users.
-- the problem is that 2 of the users are in many communities,
-- and that inner join causes one row for each community they're in.
-- so the result set only has 2 distinct users when there should be 30.
--
-- duplicates are being removed at some point in the code, but the
-- limit should be applied after that, not before.
SELECT
"user"."name",
"user"."email",
"user"."id",
"users_community"."community_id" AS "___community_id"
FROM "users" AS "user"
INNER JOIN "users_community" ON "users_community"."users_id" = "user"."id"
WHERE "user"."id" IN (
SELECT "users_community"."users_id"
FROM "users_community"
WHERE "users_community"."community_id" = '9'
) LIMIT 30 OFFSET 0
module.exports = {
attributes: {
name: 'string',
email: 'string',
communities: {
collection: 'community',
via: 'users',
through: 'communityuser'
}
},
tableName: 'users',
autoCreatedAt: false,
autoUpdatedAt: false
};
@jettwein
Copy link

Were you able to solve this issue? Also, have you had luck actually posting to POST /community/:id/users? I'm finding that I have to set these routes up myself; have not had any luck getting Waterline to handle it natively.

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