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

Incorrect relationships created with conditional unique constraint #1436

Open
hamstah opened this issue Jan 20, 2025 · 0 comments
Open

Incorrect relationships created with conditional unique constraint #1436

hamstah opened this issue Jan 20, 2025 · 0 comments

Comments

@hamstah
Copy link

hamstah commented Jan 20, 2025

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant