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
[bug?] Creating with associations #4708
Comments
Known issue i'm afraid, not totally sure how we fix it since validations run before the nested creates.. |
Could possible move the first step of nested create to before validation. |
As for transaction, I removed it for exmaple simplicity, but anyway even with transaction it does not work, if you meant that. Maybe it would be possible to just use the trick with "fake id", but behind the scenes? When building Model with options.include = [...] just set appropriate foreign key fields to some fake values (like Or maybe it would be possible to just exclude those fields from validation by using standard options? |
This is my idea (it works for fixed 'AddressId' and example above): Model.prototype.create = function(values, options) {
options = optClone(options || {});
var instance = this.build(values, {
isNewRecord: true,
attributes: options.fields,
include: options.include,
raw: options.raw,
silent: options.silent
})
options.skip = options.skip || [];
options.skip.push('AddressId'); // <------------- skip foreign key from validation
return instance.save(options);
}; However I am not familiar with Associations yet and I am not sure how to get appropriate foreign keys from options.include = [...] |
Skipping validations for fields we know will be supplied could be a good "quick" fix. |
I have also encountered this issue myself and have created a repro for it here also before finding this I had written a very detailed question on the issue on Stack Overflow here. I feel that this is a pretty fatal flaw in the library and trying to find a solution or workaround is very difficult. I will go with the above suggestions for now, but this really seems like a big issue to fix. Also in my case I am using TypeScript to help with static analysis and error checking. Adding in a fake property requires me to change the interface defining my model to include the fake property which then breaks the concept of using the interface. Needing to have a fake property that is nullable in the production code but required in the test code means having to remember that it is a fake property... |
It's definitely a flaw, but nested create is a rather new feature - And contributions are certainly always welcome :) Should really just come down to looping over belongsTo nested creates before validate and flagging them for skip. |
Got hit by this :) |
Dealing with this right now as well. Tried using a transaction but, as was pointed out earlier, that doesn't work =/ |
@mickhansen most probably I am doing something wrong but I am still getting the same error even on v.5.0.0-beta.9 Items = db.define('items', {
id: {
type: Sequelize.INTEGER,
allowNull: true,
primaryKey: true
},
name: {
type: Sequelize.STRING,
allowNull: false
},....
});
ItemSettings = db.define('settings', {
item_id: {
type: Sequelize.INTEGER,
allowNull: false,
primaryKey: true
},
photo: {
type: Sequelize.STRING,
allowNull: false
},....
});
Settings = Items.hasOne(ItemSettings, {
foreignKey: 'item_id',
as: 'setting'
});
Item.create({
name: 'test'
setting: { photo: 'file.jpg' }
}, {
include: [
Settings
]
}) The response is : { SequelizeValidationError: notNull Violation: settings.item_id cannot be null |
This error seems to occur without associations also. |
Hi guys. Before: const email = sequelize.define('user', { user.hasMany(email, { email.belongsTo(user, { after: const email = sequelize.define('user', { user.hasMany(email, { email.belongsTo(user, { Ps: I'm still testing because I own more than 40 tablelas, but the ones I tested worked without problems. |
Didn't work for me, there is other issues that haven't been addressed by Sequelize people either, so probably going to drop it all together, to much hassle. I don't have any associations |
@Klavic it's working for me, just removed property "allowNull" from an Association model. But a table was created with "null" default value. I think you have the same issue and this is a bug of sequelize library. |
I'm experiencing the same issue as @ghs with nested/assocations create with sequelize@4.42.1, dialect = mysql. If the assocation's foreignKey does not allow nulls (i.e. |
Maybe you are right but it still a bug in sequelize ( |
Any solution for this issue? =S |
Still got this issue with sequelize@6.3.5... when I try to delete an association like Bar.deleteFoo() and Foo has barId as allowNull as false. |
Hey, if you want to try to solve this problem without change the allowNull: true, you can try to apply this |
Try several solutions and it turns out that you were doing it wrong, Or if they want to create Address it would be as follows. return Address.create({ you may have a nickname you must put the [address]: this.belongsTo(Address, {as: 'address', foreignKey: 'AddressId'}); |
I want to create Company instance along with associated Address:
Output:
Quick workaround:
Output (works!):
The text was updated successfully, but these errors were encountered: