Answer the question
In order to leave comments, you need to log in
Write access check?
Hello. Tell me please. How to solve such a case correctly:
1) there is a sign with users, but users can belong to other users
CREATE TABLE public."Users" (
id serial NOT NULL,
"parentId" int4 NULL,
email varchar(100) NOT NULL DEFAULT ''::character varying,
CONSTRAINT "Users_email_key" UNIQUE (email),
CONSTRAINT "Users_pkey" PRIMARY KEY (id),
CONSTRAINT "Users_parentId_fkey" FOREIGN KEY ("parentId") REFERENCES "Users"(id) ON DELETE CASCADE,
);
CREATE TABLE public."Cartridges" (
id serial NOT NULL,
code varchar(9) NOT NULL DEFAULT ''::character varying,
printed int4 NOT NULL DEFAULT 0,
"userId" int4 NULL,
CONSTRAINT "Cartridges_pkey" PRIMARY KEY (id),
CONSTRAINT "Cartridges_userId_fkey" FOREIGN KEY ("userId") REFERENCES "Users"(id) ON DELETE CASCADE
);
WITH RECURSIVE "UsersTree" AS (
SELECT
id,
email,
"parentId"
FROM "Users"
WHERE id = 4
UNION ALL
SELECT
"Users".id,
"Users".email,
"Users"."parentId"
FROM "Users"
INNER JOIN "UsersTree" ON "Users"."parentId" = "UsersTree".id
)
update "Cartridges" set printed = 5
from "UsersTree" where "Cartridges".id = 2 and "UsersTree".id = "Cartridges"."userId"
returning *
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question