Repository navigation
Description
Issue Description
When saving a model, if an hasOne association is specified, the field should be updated instead of complaining that it is null.
Describe the solution you'd like
class User extends Sequelize.Model {
static init(sequelize) {
super.init({
name: { type: Sequelize.STRING },
roleId: { type: Sequelize.INTEGER }
}, {
tableName: 'usr_users',
sequelize
});
}
}
class Role extends Sequelize.Model {
static init(sequelize) {
super.init({
name: { type: Sequelize.STRING }
}, {
tableName: 'usr_roles',
sequelize
});
}
}
const sequelize = new Sequelize(....);
User.init(sequelize);
Role.init(sequalize);
User.Role = User.hasOne(Role, { as: 'role', foreignKey: 'id', sourceKey: 'roleId' });
Role.Users = Role.hasMany(User, { as: 'users', foreignKey: 'roleId', sourceKey: 'id' });
await Role.create({ name: 'admin' });
// -> { id: 1, name: 'admin' }
....
await User.create({ name: 'Bob', role: { id: 1, name: 'admin' }, { include: User.Role });
// Expected:
// -> { id: 1, name: 'Bob', roleId: 1 }
// Actual:
// Error: Field 'roleId' doesn't have a default valueWhy should this be in Sequelize
This feature is already implemented for belongsTo and belongsToMany relations, but hasOne and hasMany has to manually be set. Having both roleId and role.id set on the model is redundant, and especially when having hasMany. In the latter case, this use case is not possible without manually create, update the associations, then fetch the models again or reconstruct it :
await Role.create( { name: 'newRole', users: [ .... many users ] }, { include: [ Role.Users ] });In the above example, it should be expected that the created (and returned) model would be equal to
await Role.findOne({ where: { id: ... }, include: [ Role.Users ]);Describe alternatives/workarounds you've considered
At the moment, this feature is only possible if the model is fired created, then each association manually created, and then the final model constructed from the resulting data. This is even worse when the model is updated; each association has to be synchronized manually.
Additional context
None.
Issue Template Checklist
Is this issue dialect-specific?
- No. This issue is relevant to Sequelize as a whole.
- Yes. This issue only applies to the following dialect(s): XXX, YYY, ZZZ
Would you be willing to resolve this issue by submitting a Pull Request?
- Yes, I have the time and I know how to start.
- Yes, I have the time but I don't know how to start, I would need guidance.
- No, I don't have the time, although I believe I could do it if I had the time... [*]
- No, I don't have the time and I wouldn't even know how to start.
[*] I just started using Sequelize, so I am not entirely comfortable with the API just yet.
Activity
papb commentedon Jan 16, 2020
Hello, can you please take a look on how this issue relates to #11836? Thanks
github-actions commentedon Nov 5, 2021
This issue has been automatically marked as stale because it has been open for 7 days without activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment or remove the "stale" label. 🙂