Skip to content

Fix unique constraint error of generated columns #195

@martinsellergren

Description

@martinsellergren

Using a unique constraint on a "GENERATED ALWAYS AS ( generation_expr ) STORED" - column, gives you:

Error: Unique constraint "_the_key" violated for
model "model" on fields (_field) with values ().

This is because
const values = constraint.fields.map((c) => props.modelData[c]);
is [ undefined ], in this case.

Suggested fix by doing this update to constraints.ts:

before:

  for (const constraint of sortedConstraints) {
    // We skip the constraint if it contains null values
    if (
      constraint.fields.some((c) => props.modelData[c] === null)

after:

for (const constraint of sortedConstraints) {
    // We skip the constraint if it contains null values
    if (
      constraint.fields.some((c) => props.modelData[c] === null || props.modelData[c] === undefined)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions