Skip to content

Update fields automatically from association when saving model #11699

Description

@yanickrochon

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 value

Why 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

  1. added
    type: feature
    existing workaround
    on Jan 9, 2020
  2. papb commented on Jan 16, 2020

    @papb
    Member

    Hello, can you please take a look on how this issue relates to #11836? Thanks

  3. github-actions commented on Nov 5, 2021

    @github-actions
    Contributor

    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. 🙂

  4. added theissue type on Mar 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    existing workaroundtype: feature

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions