-
Notifications
You must be signed in to change notification settings - Fork 376
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
No associations defined error #398
Comments
I confirm the same error. |
You both have the params in the wrong order, see the README on the repo // Association name, Model, Extra params, Options
Patient.hasMany('doctors', Doctor, { why: String }, { reverse: 'patients' }); |
I've rechecked my code, and my parameters are specified as you quote them, I will correct my comment. |
simmelar approach, same error: Events.hasMany('imageresources', Imageresource.model(), {}, {
autoFetch: true
}); i create an event having no image associations defined, that works... update: function (id, data, cb) {
Events.get(id, function (err, obj) {
if (err) return cb(err);
console.log(obj, '-->', data);
obj.save(data, cb);
});
} the log looks like the following, obj.save throws error... { title: [Getter/Setter],
seo: [Getter/Setter],
text: [Getter/Setter],
date_from: [Getter/Setter],
date_till: [Getter/Setter],
id: [Getter/Setter],
imageresources: [] } '-->' { date_from: '2013-12-01 21:12',
date_till: '2013-12-01 21:12',
title: 'sadasd',
text: 'asdAsdasd',
seo: '17-sadasd' } error trace: |
any news on that? or do i have to search older versions of this until i find a working one? |
I was trying to recreate this bug, but haven't managed to so far. Below is my full test code. // node-orm2/test.js
var async = require('async');
var orm = require('./lib/ORM');
var dropSync = function (models, done) {
if (!Array.isArray(models)) models = [models];
async.eachSeries(models, function (item, cb) {
item.drop(function (err) {
if (err) throw err
item.sync(cb);
});
}, done);
};
orm.connect('postgres://user:[email protected]/database?debug=true', function (err, db) {
if (err) throw err;
var Address = db.define('address', { country : String, state: String });
var User = db.define('user', { name : String });
User.hasMany('addresses', Address, {});
var done = function () {
console.log("done");
db.close();
}
dropSync([Address, User], function (err) {
if (err) throw err;
var user = User.create({ name: "John" }, function (err, user) {
if (err) throw err;
var address = Address.create({ country: 'Atlantis' }, function (err, address) {
if (err) throw err;
address.state = 'NTT'
address.save(function (err) {
if (err) throw err;
user.setAddresses(address, function (err) {
if (err) throw err;
user.name = "John Smith";
user.save(function (err) {
if (err) throw err;
done();
});
});
});
});
});
});
}); |
I had same error. My model was
it was crushing on
but only when
|
I have the same problem. var Person = db.define('personnel', {
firstname : String,
lastname : String,
company_id : Number,
createdAt : String
});
Person.hasOne('company', db.models.companies, { required: true, reverse: 'personnel', autoFetch: true });
Person.hasMany('roles', db.models.roles, {}, {autoFetch: true});
Person.hasMany('groups', db.models.groups, {}, {autoFetch: true}); When I try to update the name, it fails because roles and groups have not been added yet. So when I delete those it allowes me to save. req.models.personnel.get(req.params.id, function (err, person) {
if(err) return res.send(400, err);
person.firstname = req.body.firstname;
person.lastname = req.body.lastname;
if(!person.roles[0]) delete person.roles;
if(!person.groups[0]) delete person.groups;
if(!person.shifts[0]) delete person.shifts;
person.save(function (err) {
if(err) res.send(400, err);
return res.send(200, 'ok');
});
}); |
I've got a failing test case.
Something still isn't right though; |
This seems to get one step further, but as you mentioned, I'm having issues when it then tries to add an association:
Causes
|
Apologies- it obviously didn't like me passing an array of IDs as the first parameter. Setting it to an array of Models fixed that. I can't seem to replicate the clearing of associations that weren't initially fetched FWIW |
You may be able to see it executing a It would be nice if it accepted an array of ids too.. |
@dxg Wow - now I see what you mean. If I create multiple items (booking in the below code), it's now deleting all relationships with the workers... (orm/mysql) INSERT INTO I can kind of get why it's doing the first DELETE, but by it doing the second one (bear in mind I'm creating the booking object, not the worker one at this point), it clears any previous associations. |
We'll need to fix this for the next release. I'm not sure how yet, as auto saving associations is currently the default behaviour, but will need to think about it. |
Has there been any progress on this issue? I am having the same problems as many of the developers above. |
The underlying issue has been fixed on master, however the issue discussed with gileze33 above remains. I need to think of a good solution - and might need to make some breaking changes to achieve this - not sure yet. |
@gileze33 what sort of association do you have that it deletes twice? For me it only deletes once. The single delete isn't really a problem - it's expected given the current behaviour of auto-saving associations. It can be redundant when associations are not changed though.. |
Issue should be completely resolved now - including the unnecessary deletes discussed above. |
I have a
users
model withhasMany('addresses', {description:String}, db.model.addresses)
when I try to useuser.setAddresses()
oruser.addAddresses()
it throws an error saying "No associations defined".I checked out the code line that threw the error (line 398, Many.js file) and it is an if statement checking if
Associations.length === 0
, but as it seems, Associations never gets an object pushed, it seems like it would always have length = 0.What could I be doing wrong ?
The text was updated successfully, but these errors were encountered: