Skip to content
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

Fix unique constraint error of generated columns #195

Open
martinsellergren opened this issue Aug 16, 2024 · 2 comments
Open

Fix unique constraint error of generated columns #195

martinsellergren opened this issue Aug 16, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@martinsellergren
Copy link

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)
@martinsellergren martinsellergren added the bug Something isn't working label Aug 16, 2024
@peterp
Copy link
Collaborator

peterp commented Sep 2, 2024

@martinsellergren Would you consider opening a PR to fix this?

@martinsellergren
Copy link
Author

@martinsellergren Would you consider opening a PR to fix this?

Sure, but unfortunately not gonna have the time to write tests. Would you add me as a collaborator and I'll do it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants