An example on how to setup Many-to-Many relationships through a junction table with it's own model definition, enabling individual attributes and filtering on the relationship.
Last active
August 29, 2015 14:01
-
-
Save mphasize/aeaff696132357797e0b to your computer and use it in GitHub Desktop.
Sails-Many-to-Many through
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = { | |
attributes: { | |
[ ... ] | |
owners: { | |
collection: "user", | |
via: "apps", | |
through: "team" | |
} | |
[ ... ] | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = { | |
tableName: 'team', | |
tables: [ 'user', 'app' ], | |
junctionTable: true, | |
attributes: { | |
id: { | |
primaryKey: true, | |
autoIncrement: true, | |
type: 'integer' | |
}, | |
user: { | |
columnName: 'user', | |
type: 'integer', | |
foreignKey: true, | |
references: 'user', | |
on: 'id', | |
via: 'app', | |
groupBy: 'user' | |
}, | |
app: { | |
columnName: 'app', | |
type: 'integer', | |
foreignKey: true, | |
references: 'app', | |
on: 'id', | |
via: 'user', | |
groupBy: 'app' | |
}, | |
role: { | |
type: "string" | |
} | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = { | |
attributes: { | |
[ ... ] | |
apps: { | |
collection: "app", | |
via: "owners", | |
through: "team" | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment