Incorrect relationships created with conditional unique constraint
Author: hamstahCreated Jan 20, 2025Updated Apr 4, 2025
Labelsbughelp wanted
Using v4.18.0 (previously tested on 4.16 too) Using the postgres driver (but not sure it's postgres specific)
given the schema generated by
CREATE TABLE A (
id serial PRIMARY KEY,
content text NOT NULL
);
CREATE TABLE b (
id serial PRIMARY KEY,
a_id INT NOT NULL REFERENCES A(id) ON
DELETE
CASCADE,
content text NOT NULL
);
CREATE UNIQUE INDEX unique_a_id ON b(a_id)
WHERE
content = 'test';The generated relationship on A is
...
// aR is where relationships are stored.
type aR struct {
B *B `boil:"B" json:"B" toml:"B" yaml:"B"`
}
...ie it created a 1:1 relationship, but it should be a 1:many relationship as there could be multiple B linked to the same A, as long as at most one of them has the test content.
if I remove the unique constraint I get the correct relationship
...
// aR is where relationships are stored.
type aR struct {
BS BSlice `boil:"BS" json:"BS" toml:"BS" yaml:"BS"`
}
...It seems like the issue if with the relationship logic trying to handle the unique constraint and convert the relationship from 1:many to 1:1 but not accounting for the conditional part of it.
Source: aarondl/sqlboiler