sequelize: notNull Violation when creating an association with the property filled.

Issue Description

I have a model, Project, and another one, Testy, which has a required column (amount).

Even if this column is filled on a built but not persisted Testy, then creating the association through the association helpers fails with this error:

Unhandled rejection SequelizeValidationError: notNull Violation: Testy.amount cannot be null

What are you doing?

Here are two very simplified Models built with sequelize-typescript. While the syntax might be foreign, the example is straightforward enough.

@Table({
  timestamps: true,
})
class Testy extends Model<Testy> {
  @AllowNull(false)
  @Column(DataType.DECIMAL(12, 2))
  amount: number;

  @ForeignKey(() => Project)
  @Column
  projectId: number;

  @BelongsTo(() => Project)
  project: Project;
}

@Table({
  timestamps: true,
})
class Project extends Model<Project> {
  @HasMany(() => Testy)
  testies: Testy;
}

Project.create({}).then((project) => {
    const testy = Testy.build({ amount: 3 })

    project.createTesty(testy); // THIS LINE WILL PRODUCE ERROR
})

What do you expect to happen?

The expectation is that because the amount property is provided, then the Testy will be persisted to the database with the projectId properly filled in.

What is actually happening?

Unhandled rejection SequelizeValidationError: notNull Violation: Testy.amount cannot be null

Environment

  • Sequelize version: 5.21.5
  • Node.js version: 12.10.0
  • Operating System: macOS Catalina 10.15.3
  • If TypeScript related: TypeScript version: 3.7.5

Issue Template Checklist

How does this problem relate to dialects?

  • I think this problem happens regardless of the dialect.
  • I think this problem happens only for the following dialect(s):
  • I don’t know, I was using PUT-YOUR-DIALECT-HERE, with connector library version XXX and database version XXX

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.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 17 (8 by maintainers)

Most upvoted comments

@dilizarov No it won’t work with build instance, unless you have built that instance with PK.