create table users ( id bigserial not null, telegram_user_id bigint not null, first_name character varying (64) not null, last_name character varying (64), username character varying (32), primary key (id), unique (telegram_user_id) ); create table poll_schemas ( id bigserial not null, name character varying (32) not null, question character varying (255) not null, primary key (id), unique (name) ); create table poll_options ( id bigserial not null, poll_schema_id bigint not null, name character varying (100) not null, ordinal bigint, primary key (id), foreign key (poll_schema_id) references poll_schemas on delete cascade on update cascade, unique (poll_schema_id, name), unique (poll_schema_id, ordinal) ); create table polls ( id bigserial not null, telegram_message_id bigint not null, telegram_poll_id text not null, poll_schema_id bigint not null, created_at timestamp default now() not null, is_complete boolean default false not null, primary key (id), foreign key (poll_schema_id) references poll_schemas on delete cascade on update cascade, unique (telegram_message_id), unique (telegram_poll_id) ); create table poll_answers ( id bigserial not null, poll_id bigint not null, user_id bigint not null, poll_option_id bigint not null, primary key (id), foreign key (poll_id) references polls on delete cascade on update cascade, foreign key (user_id) references users on delete cascade on update cascade, foreign key (poll_option_id) references poll_options on delete cascade on update cascade, unique (poll_id, user_id, poll_option_id) ); insert into poll_schemas (name, question) values ('mood', 'Оцените свое состояние на текущую минуту'); insert into poll_options (poll_schema_id, name, ordinal) values (1, '😄', 0); insert into poll_options (poll_schema_id, name, ordinal) values (1, '🤪', 1); insert into poll_options (poll_schema_id, name, ordinal) values (1, '🫠', 2); insert into poll_options (poll_schema_id, name, ordinal) values (1, '☠️', 3); insert into poll_options (poll_schema_id, name, ordinal) values (1, '🤡', 4); insert into poll_options (poll_schema_id, name, ordinal) values (1, '😟', 5); insert into poll_options (poll_schema_id, name, ordinal) values (1, '😩', 6); insert into poll_options (poll_schema_id, name, ordinal) values (1, '😡', 7); insert into poll_schemas (name, question) values ('lunch', 'Какие у вас планы на обед?'); insert into poll_options (poll_schema_id, name, ordinal) values (2, '🍽️ Пойду в общепит', 0); insert into poll_options (poll_schema_id, name, ordinal) values (2, '📦 Хочу заказать в офис', 1); insert into poll_options (poll_schema_id, name, ordinal) values (2, '🥪 Всё своё ношу с собой', 2); insert into poll_options (poll_schema_id, name, ordinal) values (2, '😴 Хочу спать', 3);