Skip to content

Instantly share code, notes, and snippets.

@abevoelker
Created January 3, 2017 04:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abevoelker/ecaea89e33ff67e943f2ad1aea3c9552 to your computer and use it in GitHub Desktop.
Save abevoelker/ecaea89e33ff67e943f2ad1aea3c9552 to your computer and use it in GitHub Desktop.
CREATE TABLE products (
id BIGSERIAL,
title TEXT NOT NULL,
hashid TEXT NOT NULL
);
CREATE FUNCTION products_pre_insert() RETURNS trigger AS $$
BEGIN
NEW.hashid := gen_hashid('products_secret_salt_here', 3, NEW.id);
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER products_pre_insert BEFORE INSERT ON products FOR EACH ROW EXECUTE PROCEDURE products_pre_insert();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment