Skip to content

Incorrect relationships created with conditional unique constraint #1436

Open
@hamstah

Description

@hamstah

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions