-
Notifications
You must be signed in to change notification settings - Fork 34
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
Many to Many relations no longer working #44
Comments
@Dalanie, hopefully this isn't still an issue for you, but I had this problem as well. I can't find the issue, but it seems to be in the Waterline core, as I have issues with .add() manually. The fix, seems to be to use the "through" associations. Minor bugs there too, in that, autoCreatedAt and autoUpdatedAt can not be turned off on the middle-man table... But everything else works as intended otherwise. |
@neonexus @Dalanie I got the same error while writing fixtures for unit test, here are my fixtures, Project and student have many to many association: Project.json:
[{
id: 1,
name: 'abc',
students: [1]
}]
Student.json:
[{
id: 1,
name: 'xyz',
projects: [1]
}] I get the following error: Details: [ { type: 'insert',
collection: 'project_students__student_projects',
criteria: { student_projects: 1, project_students: 1 },
values: { student_projects: 1, project_students: 1 },
err: [Error: Associated Record For student with id = 1 No Longer Exists] } ] my bootstrap.spec.js is as follows: var Sails = require('sails'),
Barrels = require('barrels'),
sails;
before(function(done) {
// Increase the Mocha timeout so that Sails has enough time to lift.
this.timeout(30000);
Sails.lift({
// configuration for testing purposes
log: {
level: 'error'
},
models: {
connection: 'test',
migrate: 'drop'
}
}, function(err, server) {
sails = server;
if (err) return done(err);
// here you can load fixtures, etc.
// Load fixtures
var barrels = new Barrels();
// Save original objects in `fixtures` variable
fixtures = barrels.data;
// Populate the DB
barrels.populate(['project', 'student'], function(err) {
if (err){
return done(err);
}
else {
return done(null, sails)
}
}, false);
});
});
after(function(done) {
// here you can clear fixtures, etc.
// console.log('\n');
Sails.lower(done);
}); What is the possible solution for this, I am using |
@aayush-practo Can I see your models? |
@neonexus, here are the models Project.js
module.exports = {
autoPK : true,
migrate : 'safe',
autoCreatedAt: true,
autoUpdatedAt: true,
attributes: {
name: {
type: 'string'
},
students: {
collection: 'student',
via : 'projects'
}
},
tableName: 'projects'
}
Student.js
module.exports = {
autoPK : true,
migrate : 'safe',
autoCreatedAt: true,
autoUpdatedAt: true,
attributes: {
name: {
type: 'string'
},
projects: {
collection: 'project',
via : 'students'
}
},
tableName: 'students'
} |
@aayush-practo, you need to use "through" associations as I mentioned previously. There is a bug down in the Waterline core that needs to be addressed, and this is the way around that. In other words, add the following model to your set (name it ProjectStudent.js): module.exports = {
attributes: {
// force ID as first column, autoPk in Sails adds it to the end
id: {
type: 'int',
primaryKey: true,
autoIncrement: true
},
project: {
model: 'project'
},
student: {
model: 'student'
}
}
}; Then Project.js: module.exports = {
autoPK : true,
migrate : 'safe',
autoCreatedAt: true,
autoUpdatedAt: true,
attributes: {
name: {
type: 'string'
},
students: {
collection: 'student',
via : 'project',
through : 'projectstudent'
}
},
tableName: 'projects'
} And Student.js: module.exports = {
autoPK : true,
migrate : 'safe',
autoCreatedAt: true,
autoUpdatedAt: true,
attributes: {
name: {
type: 'string'
},
projects: {
collection: 'project',
via : 'student',
through : 'projectstudent'
}
},
tableName: 'students'
} |
is this still a bug in waterline and a requirement today? I tried doing as @neonexus suggested, but getting this error models/ServerComponent.js
models/Server.js
models/Component.js
|
After updating all libraries, it seems like the many to many relations are no longer working for me. Do you happen to know if any lib-update might effect barrels?
These are my dependencies:
I populate all models before my 'main'-model is populated. This 'main'-model contains all references to the other models, e.g:
{ "id": 1, "title": "13In est risus, auctor sed,", "clearance": 1, "project": 13, "ipType": 0, "createdUser": 12, "authors": [ 10, 6 ], "published": "1997-01-25", "type": 5, "files": 627, "released": false },
My other json files (such as Project, Clearance, ...) do not contain any information about this 'main' model. I did not need it before the update.
One-To-One relations work fine, one-to-many as well. Only many-to-many don't seem to work anymore.
Do you have any workaround?
The text was updated successfully, but these errors were encountered: