1

I have the permissions table in postgres table

CREATE TABLE public.permissions
(
permission_id integer NOT NULL,
crud_signature text COLLATE pg_catalog."default" NOT NULL,
object_id uuid NOT NULL,
object_type text COLLATE pg_catalog."default" NOT NULL,
include_subcategories boolean NOT NULL,
CONSTRAINT permissions_pkey PRIMARY KEY (permission_id)
)
WITH (
OIDS = FALSE
)
TABLESPACE pg_default;

ALTER TABLE public.permissions
OWNER to postgres;

and have ROLES table. Each role has multiple permission.

CREATE TABLE public.roles
(
role_id integer NOT NULL,
role_name text COLLATE pg_catalog."default" NOT NULL,
role_description text COLLATE pg_catalog."default",
permissions integer[],
CONSTRAINT roles_pkey PRIMARY KEY (role_id)
)
WITH (
OIDS = FALSE
)
TABLESPACE pg_default;

ALTER TABLE public.roles OWNER to postgres;

When I try to make a foreign keys column like permissions of type integer[] Posgres gives me an error that theese fields have incompartables types - integer and integer[]

What should I do?

3
  • Please include enough sample input and output data in your question to make it clear what you are trying to do here. Commented Apr 15, 2019 at 5:31
  • I try to make a permissions field in ROLES table a foreign key. And it should be an array of permissions keys Commented Apr 15, 2019 at 5:33
  • I recommend making a design change, q.v. my answer below. Commented Apr 15, 2019 at 5:37

1 Answer 1

2

I would actually recommend not using arrays here. Instead, use a junction table to represent each role-permission relationship, something like this:

CREATE TABLE roles_permissions (
    role_id integer NOT NULL,
    permission_id integer NOT NULL,
    CONSTRAINT rp_pkey PRIMARY KEY (role_id, permission_id)
);

Then modify your current roles table by removing the array of permissions. Your current approach uses unnormalized data, because you are trying to store a set of permissions in an array for each separate role.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.