Comments (7)
Awesome! Thanks Matheus
from openrecord.
Hi Matheus,
without further investigations I would say that your relationship names and the names in your include
statement are not the same. clutch_module
vs clutch_modules
. I think thatโs the poblem!
I would suggest to put your hasMany
relations names in plural - itโs nicer to ready.
Thanks,
Philipp
from openrecord.
Nope, doesn't work either:
module.exports = function ClutchUsers () {
this.hasMany('clutch_user_permissions')
.hasMany('clutch_modules', {
through: 'clutch_user_permissions',
primary_key: 'id',
foreign_key: 'user'
})
.scope('active', function() {
this.where({status: 1})
})
}
The "Can't find relation "clutch_modules" for ClutchUsers" persists...
from openrecord.
Hi Matheus,
yep, I've found the problems:
- The
hasMany through
relation need the name of the target relationclutch_module
- something that could be done better by OpenRecord (try the singular version of the relation name) - The
hasMany through
relation does not need to know the primary or foreign key - it uses it from it's target relation. - The relations of the
ClutchUserPermission
module needed extra info regarding it's relation keys (module
anduser
). OpenRecord was searching for auser_id
andmodule_id
field.
here is a working example (with an sqlite3 store)
var OpenRecord = require('./store')
var store = new OpenRecord({
type: 'sqlite3',
file: 'db-test.sqlite3'
})
/*
CREATE TABLE clutch_users(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, status integer);
CREATE TABLE clutch_user_permissions(id INTEGER PRIMARY KEY AUTOINCREMENT, user INTEGER, module INTEGER);
CREATE TABLE clutch_modules(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT);
INSERT INTO clutch_users(name, status) VALUES ('A', 1);
INSERT INTO clutch_users(name, status) VALUES ('B', 0);
INSERT INTO clutch_modules(name) VALUES ('M1');
INSERT INTO clutch_modules(name) VALUES ('M2');
INSERT INTO clutch_user_permissions(user, module) VALUES (1, 1);
INSERT INTO clutch_user_permissions(user, module) VALUES (1, 2);
INSERT INTO clutch_user_permissions(user, module) VALUES (2, 2);
*/
store.Model('ClutchUser', function ClutchUsers() {
this.hasMany('clutch_user_permissions', {foreign_key: 'user'})
.hasMany('clutch_modules', {through: 'clutch_user_permissions', relation: 'clutch_module'})
.scope('active', function() {
this.where({status: 1})
})
})
// ClutchUserPermissions.model.js
store.Model('ClutchUserPermission', function() {
this.belongsToMany('clutch_user', {primary_key: 'user'})
.belongsToMany('clutch_module', {primary_key: 'module'})
})
// ClutchModules.model.js
store.Model('ClutchModule', function() {
this.hasMany('clutch_user_permissions', {foreign_key: 'module'})
.hasMany('clutch_users', {through: 'clutch_user_permissions'})
})
store.ready(function(){
const ClutchUsers = store.Model('ClutchUser')
ClutchUsers.active().include('clutch_modules').exec((records) => {
console.log(records)
})
})
from openrecord.
I see in your example that the clutch_user_permissions
table you created has an id
column. Mine doesn't, only user
and module
columns, that are foreign keys pointing to the other tables. Tried your solution, didn't work either... Should I be using an id
column as well?
Edit: tried adding an id
column to the table, still doesn't work
from openrecord.
Hi Matheus,
nope, my example also works without the id
column on clutch_user_permissions
! So that shouldn't be the problem - it's just a preference of designing you schema.
If my above example does not work for you, than there is something else fishy.
You are using [email protected] right? Wich database and version do you use?
from openrecord.
Ok so I deleted everything and started from scratch and now it's working. Probably was some mispelling that I didn't see before. Thank you for your help and sorry for taking so much of your time. Your library works great!
Eager to see the new version and full documatation. Keep up the good work and support.
from openrecord.
Related Issues (20)
- Add documentation or examples to use with ldap HOT 4
- Publish a new release with latest dependency HOT 2
- How can i make raw sql query? HOT 1
- RangeError: Maximum call stack size exceeded HOT 1
- How to select SQL? HOT 2
- select_value as in activerecord HOT 12
- Please update CONTRIBUTING.md HOT 1
- Not returning complete record on create HOT 2
- How can one import/require the ValidationError class? HOT 1
- Help Wanted. Database query builder HOT 2
- Integer keys HOT 4
- has many through HOT 4
- Model.find() with empty argument has an unexpected return HOT 2
- Bugs when using offset / limit HOT 5
- How to initialize records from raw rows? HOT 2
- VScode Intellisense for auto generated models HOT 2
- create(data) but get null HOT 2
- Model.create({}) and Model.new({}) autoAttributes = true HOT 11
- ID conversion problem HOT 9
- mysql socketPath HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from openrecord.