Skip to content

Instantly share code, notes, and snippets.

@MbuguaCaleb
Created March 17, 2021 11:38
Show Gist options
  • Save MbuguaCaleb/77ae0fbf49da131124882185f69b4504 to your computer and use it in GitHub Desktop.
Save MbuguaCaleb/77ae0fbf49da131124882185f69b4504 to your computer and use it in GitHub Desktop.
um_permissions.sql
SELECT id, name, display_name, description, created_at, updated_at
FROM unittrust.um_permissions;
SELECT Max(id)FROM unittrust.um_permissions;
SELECT *FROM unittrust.um_permissions
WHERE name LIKE '%system-modules%';
ALTER SEQUENCE um_mod_permissions_id_seq RESTART WITH 1;
ALTER SEQUENCE um_permissions_id_seq RESTART WITH 1;
SET search_path TO unittrust;
ALTER SEQUENCE um_roles_id_seq RESTART WITH 1;
@MbuguaCaleb
Copy link
Author

  1. For a non-existing column

-- auto-increment constraint for a new column
ALTER TABLE public.products
ADD COLUMN id SERIAL PRIMARY KEY;

@MbuguaCaleb
Copy link
Author

-- create sequence

  1. For an existing column that got no values in the table

CREATE SEQUENCE public_products_id_seq OWNED BY public.products.id;

-- use sequence for the target column
ALTER TABLE public.products ALTER COLUMN id SET DEFAULT nextval('public_products_id_seq');

@MbuguaCaleb
Copy link
Author

  1. For an existing column that already got some values in the table

-- create sequence
CREATE SEQUENCE public_products_id_seq OWNED BY public.products.id;

-- set the current value of the sequence to the max value from that column
-- (id column in this scenario)
SELECT SETVAL('public_products_id_seq', (select max(id) from public.products), false)

-- use sequence for the target column
ALTER TABLE public.products ALTER COLUMN id SET DEFAULT nextval('public_products_id_seq');

@MbuguaCaleb
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment