volatiletech / sqlboiler

Generate a Go ORM tailored to your database schema.
BSD 3-Clause "New" or "Revised" License
6.73k stars 544 forks source link

Constraint with is not null on two columns #502

Closed GalGavu closed 5 years ago

GalGavu commented 5 years ago

What version of SQLBoiler are you using (sqlboiler --version)?

SQLBoiler v3.2.0

If this happened at generation time what was the full SQLBoiler command you used to generate your models? (if not applicable leave blank)

If this happened at runtime what code produced the issue? (if not applicable leave blank)

go test -v ./models

What is the output of the command above with the -d flag added to it? (Provided you are comfortable sharing this, it contains a blueprint of your schema)

Please provide a relevant database schema so we can replicate your issue (Provided you are comfortable sharing this)

constraint one_id_or_two_id_present
        check ((one_id IS NOT NULL) OR (two_id IS NOT NULL))

Further information. What did you do, what did you expect?

The issue is pretty obvious:

models: unable to update all in your_table slice: pq: new row for relation "your_table" violates check constraint "one_id_or_two_id_present"

I know what it means. I'm wondering how to approach this. In one of the issues I found that I could overrride it with a type (or import) but I'm not sure how to do this. I mean should I create a different randomize that will always pick one of those two ids as not null? If yes can you give me a hint on where to start?

aarondl commented 5 years ago

Crazy constraints will always murder our tests. It's actually not very plausible to try to extract them from the database and generate the tests to keep care of each constraint placed on the database, there's just too many possibilities so as to make it not worth it. Same with custom user types.

The tests are a smoke test/canary. You run them and see what problems come up if any and decide whether or not you can use sqlboiler as a result. They're not really meant to be run in CI or anything although if your database was simple enough maybe you could!

The way around this is to decide whether or not to use sqlboiler based on your test results and disable them for your project as they're incompatible.