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

Constraint violation on many to many join table #8882

Closed
melohagan opened this issue Nov 30, 2022 · 5 comments
Closed

Constraint violation on many to many join table #8882

melohagan opened this issue Nov 30, 2022 · 5 comments
Assignees
Labels
bb-relationships Relating to table relationships bug Something isn't working env - production Bug found in production externaldb Relating to datasource plus

Comments

@melohagan
Copy link
Collaborator

Hosting

  • Self
    • Method: docker compose
    • Budibase Version: 2.1.41
    • App Version: 2.1.41

Describe the bug
Consider the following setup: Many employees -> Many skills
Two employees have many skills. Some overlap, some don't.
Try to change an employees skills through an auto-generated screen will result in constraint violation on the join table.

To Reproduce
Steps to reproduce the behavior:

  1. Setup an Employee, Skills and Join table for a many to many relationship in Postgres. (scripts below)
  2. Fetch tables in Budibase
  3. Setup relationships (Many -> Many)
  4. Create auto-generated screens for the Employee table
  5. Preview app, and unselect a skill for an employee and save
  6. See error

Expected behavior
A clear and concise description of what you expected to happen.

Screenshots
Remove 'Docker' and save
Screenshot 2022-11-30 at 16 51 05

SQL Scripts

CREATE TABLE IF NOT EXISTS public."Employee"
(
    id integer NOT NULL,
    name text COLLATE pg_catalog."default",
    CONSTRAINT "Employee_pkey" PRIMARY KEY (id)
)
WITH (
    OIDS = FALSE
)
TABLESPACE pg_default;

ALTER TABLE IF EXISTS public."Employee"
    OWNER to postgres;

INSERT INTO public."Employee" ("id", "name") VALUES (1, 'Alice');
INSERT INTO public."Employee" ("id", "name") VALUES (2, 'Bob');
CREATE TABLE IF NOT EXISTS public."Skills"
(
    id integer NOT NULL,
    name text COLLATE pg_catalog."default",
    CONSTRAINT "Skills_pkey" PRIMARY KEY (id)
)
WITH (
    OIDS = FALSE
)
TABLESPACE pg_default;

ALTER TABLE IF EXISTS public."Skills"
    OWNER to postgres;

INSERT INTO public."Skills" ("id", "name") VALUES (1, 'Docker');
INSERT INTO public."Skills" ("id", "name") VALUES (2, 'Microservices');
INSERT INTO public."Skills" ("id", "name") VALUES (3, 'Kubernetes');
INSERT INTO public."Skills" ("id", "name") VALUES (4, 'Spring');
CREATE TABLE IF NOT EXISTS public."jt_employee_skills_Skills_employee"
(
    employee_id integer,
    skills_id integer,
    id integer NOT NULL,
    CONSTRAINT "jt_employee_skills_Skills_employee_pkey" PRIMARY KEY (id)
)
WITH (
    OIDS = FALSE
)
TABLESPACE pg_default;

ALTER TABLE IF EXISTS public."jt_employee_skills_Skills_employee"
    OWNER to postgres;

insert into public."jt_employee_skills_Skills_employee" ("id", "employee_id", "skills_id") VALUES (1, 1, 1);
insert into public."jt_employee_skills_Skills_employee" ("id", "employee_id", "skills_id") VALUES (2, 1, 2);
insert into public."jt_employee_skills_Skills_employee" ("id", "employee_id", "skills_id") VALUES (3, 1, 3);
insert into public."jt_employee_skills_Skills_employee" ("id", "employee_id", "skills_id") VALUES (4, 2, 2);
insert into public."jt_employee_skills_Skills_employee" ("id", "employee_id", "skills_id") VALUES (5, 2, 3);
insert into public."jt_employee_skills_Skills_employee" ("id", "employee_id", "skills_id") VALUES (6, 2, 4);
@melohagan melohagan added bug Something isn't working sev2 - severe bb-relationships Relating to table relationships externaldb Relating to datasource plus env - production Bug found in production labels Nov 30, 2022
@mike12345567 mike12345567 self-assigned this Dec 1, 2022
@mike12345567
Copy link
Collaborator

mike12345567 commented Dec 1, 2022

Found the source of this issue - the joining table here is relatively poorly defined, the issue is that the id primary key of it isn't auto-generated, and ideally for a joining table your primary key should be a composite of the the two foreign keys, a good definition for the junction table would look like:

CREATE TABLE IF NOT EXISTS public."jt_employee_skills_Skills_employee"
(
    employee_id integer NOT NULL,
    skills_id integer NOT NULL,
    CONSTRAINT fkEmployeeId FOREIGN KEY(employee_id) REFERENCES Employee(id),
    CONSTRAINT fkSkillId FOREIGN KEY(skills_id) REFERENCES Skills(id),
    PRIMARY KEY (employee_id, skills_id)
)
WITH (
    OIDS = FALSE
)

This is the type of junction table that Budibase is designed to handle, auto-generating not null primary key fields, of any type, could be very difficult as the id field on the junction table could be a number, a string, a date etc.

mike12345567 added a commit that referenced this issue Dec 2, 2022
…ng an issue with many to many updating correctly.
@mike12345567 mike12345567 mentioned this issue Dec 2, 2022
@pankajjangid05
Copy link

Hi @mike12345567

I tried the same thing but still, it's not working. So basically I want to remove skills using budibase design and update the employee records. The employee records are getting updated and the new skills are also being added to the DB. The problem is that when I try to remove skills, it shows removed in the form UI, but the same is not updated in the database.

Please let me know if you want screenshots, schema, or anything else for your reference.

@mike12345567
Copy link
Collaborator

Hi @pankajjangid05 - there was another underlying issue which I've fixed in my PR - this will be released in the next Budibase update.

@pankajjangid05
Copy link

Hi @mike12345567

I checked the new release, but it is not working for me as expected, If you have any idea what could be the other way to do this Please let me know.

@mike12345567
Copy link
Collaborator

mike12345567 commented Dec 6, 2022

Hi @pankajjangid05 - this has not yet been released - it will be part of our main release which should occur sometime around next week. It will be in version v2.2.X. Patch versions like 2.1.<patch number> are for very minor bug fixes which don't go through QA.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bb-relationships Relating to table relationships bug Something isn't working env - production Bug found in production externaldb Relating to datasource plus
Projects
None yet
Development

No branches or pull requests

4 participants