Skip to content

Instantly share code, notes, and snippets.

@rahsheen
Last active December 31, 2019 01:47
Show Gist options
  • Save rahsheen/2fce2e5c0517c1ee44e1e42fa3484e50 to your computer and use it in GitHub Desktop.
Save rahsheen/2fce2e5c0517c1ee44e1e42fa3484e50 to your computer and use it in GitHub Desktop.
CREATE TABLE public.boxes (
id integer NOT NULL,
hand_amount money NOT NULL,
frequency integer DEFAULT 2 NOT NULL,
start_date date NOT NULL
);
CREATE SEQUENCE public.boxes_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER SEQUENCE public.boxes_id_seq OWNED BY public.boxes.id;
CREATE TABLE public.users (
uid text NOT NULL,
name text NOT NULL
);
CREATE TABLE public.users_boxes (
id integer NOT NULL,
user_id text NOT NULL,
box_id integer NOT NULL
);
CREATE SEQUENCE public.users_boxes_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER SEQUENCE public.users_boxes_id_seq OWNED BY public.users_boxes.id;
ALTER TABLE ONLY public.boxes ALTER COLUMN id SET DEFAULT nextval('public.boxes_id_seq'::regclass);
ALTER TABLE ONLY public.users_boxes ALTER COLUMN id SET DEFAULT nextval('public.users_boxes_id_seq'::regclass);
ALTER TABLE ONLY public.boxes
ADD CONSTRAINT boxes_pkey PRIMARY KEY (id);
ALTER TABLE ONLY public.users_boxes
ADD CONSTRAINT users_boxes_pkey PRIMARY KEY (id);
ALTER TABLE ONLY public.users
ADD CONSTRAINT users_pkey PRIMARY KEY (uid);
CREATE TABLE public.boxes (
id integer NOT NULL,
hand_amount money DEFAULT 0.00 NOT NULL,
frequency integer DEFAULT 2 NOT NULL,
start_date date DEFAULT now() NOT NULL
);
CREATE SEQUENCE public.boxes_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER SEQUENCE public.boxes_id_seq OWNED BY public.boxes.id;
CREATE TABLE public.user_boxes (
user_id text NOT NULL,
box_id integer NOT NULL
);
CREATE TABLE public.users (
id text NOT NULL,
name text NOT NULL
);
ALTER TABLE ONLY public.boxes ALTER COLUMN id SET DEFAULT nextval('public.boxes_id_seq'::regclass);
ALTER TABLE ONLY public.boxes
ADD CONSTRAINT boxes_id_key UNIQUE (id);
ALTER TABLE ONLY public.boxes
ADD CONSTRAINT boxes_pkey PRIMARY KEY (id);
ALTER TABLE ONLY public.user_boxes
ADD CONSTRAINT user_boxes_box_id_key UNIQUE (box_id);
ALTER TABLE ONLY public.user_boxes
ADD CONSTRAINT user_boxes_box_id_user_id_key UNIQUE (box_id, user_id);
ALTER TABLE ONLY public.user_boxes
ADD CONSTRAINT user_boxes_pkey PRIMARY KEY (user_id, box_id);
ALTER TABLE ONLY public.users
ADD CONSTRAINT users_pkey PRIMARY KEY (id);
ALTER TABLE ONLY public.boxes
ADD CONSTRAINT boxes_id_fkey FOREIGN KEY (id) REFERENCES public.user_boxes(box_id) ON UPDATE RESTRICT ON DELETE RESTRICT;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment