Skip to content

Commit

Permalink
Fix postgres weird operator error when joining by switching from ulid…
Browse files Browse the repository at this point in the history
… to text type

pksunkara/pgx_ulid#13
  • Loading branch information
micromaomao committed Jul 23, 2023
1 parent ae4a1e1 commit 94e6062
Showing 1 changed file with 28 additions and 30 deletions.
58 changes: 28 additions & 30 deletions app/src/db/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ insert into db_migration_state (db_compat_version) values (1);

-- Global configuration like prompt to use, etc.
create table global_configuration (
id ulid not null default gen_ulid() primary key,
id text not null default gen_ulid() primary key,
config json not null,
app_version text not null
);
Expand All @@ -21,12 +21,12 @@ create table captcha_challenge (
created_time timestamptz not null default now(),
solve_time timestamptz default null,
-- References chat_session
associated_session ulid default null
associated_session text default null
);

create table chat_session (
-- Not secret identifier for this chat session
session_id ulid not null default gen_ulid() primary key,
session_id text not null default gen_ulid() primary key,

-- Secret used for fetching / sending messages
session_token bytea not null unique,
Expand All @@ -35,15 +35,14 @@ create table chat_session (
last_captcha bytea default null references captcha_challenge (challenge_token) on delete set null,

-- References to stored_dialogue. Cache for determining "ignore_for_first_match"
last_matched_dialogues ulid[] not null default '{}'
last_matched_dialogues text[] not null default '{}'
);

-- alter table captcha_challenge add foreign key (associated_session) references chat_session (session_id) on delete set null;
-- The above gives ERROR: XX000: could not find commutator for operator 16397
alter table captcha_challenge add foreign key (associated_session) references chat_session (session_id) on delete set null;

create table chat_message (
id ulid not null default gen_ulid() primary key,
session ulid not null references chat_session (session_id) on delete cascade,
id text not null default gen_ulid() primary key,
session text not null references chat_session (session_id) on delete cascade,

-- enum: 0 = bot, 1 = user
msg_type int not null,
Expand All @@ -53,7 +52,7 @@ create table chat_message (
-- true if this message has been replaced by an attempted "regenerate response"
old_regenerated boolean not null default false,

-- Name of LLM used to generate this (or, for a user message, a reply to this)
-- Name of LLM used to generate this (or, for a user message, a reply to this).
generation_model text default null,

-- Number of tokens using the tokenization scheme of .generation_model
Expand All @@ -64,7 +63,7 @@ create unique index chat_msg_session_and_id on chat_message (session, id);

-- Store embeddings for user messages only, for future analytics etc.
create table chat_message_embedding (
msg ulid not null references chat_message (id) on delete cascade,
msg text not null references chat_message (id) on delete cascade,

-- Name of the embedding model
model text not null,
Expand All @@ -79,18 +78,18 @@ create table chat_message_embedding (
);

create table stored_dialogue (
id ulid not null default gen_ulid() primary key
id text not null default gen_ulid() primary key
);

create table dialogue_item (
id ulid not null default gen_ulid() primary key,
dialogue ulid not null references stored_dialogue (id) on delete cascade,
id text not null default gen_ulid() primary key,
dialogue text not null references stored_dialogue (id) on delete cascade,

-- Represents a root if null
parent ulid default null references dialogue_item (id) on delete set null,
parent text default null references dialogue_item (id) on delete set null,

-- References dialogue_phrasing. Determine which phrasing is shown in the editing UI, does not affect generation.
canonical_phrasing ulid default null,
canonical_phrasing text default null,

response text not null,

Expand All @@ -102,20 +101,19 @@ create table dialogue_item (
-- User input is matched against all phrasings for an item, and the score for
-- the item is the max score across all phrasings.
create table dialogue_phrasing (
id ulid not null default gen_ulid() primary key,
dialogue_item ulid not null references dialogue_item (id) on delete cascade,
id text not null default gen_ulid() primary key,
dialogue_item text not null references dialogue_item (id) on delete cascade,
q_text text not null,

-- A matched "counterexample" phrasing will prevent this item from being
-- matched unless another phrasing is matched first, with a higher score.
is_counterexample boolean not null default false
);

-- alter table dialogue_item foreign key (canonical_phrasing) references dialogue_phrasing (id) on delete set null;
-- The above gives ERROR: XX000: could not find commutator for operator 16397
alter table dialogue_item add foreign key (canonical_phrasing) references dialogue_phrasing (id) on delete set null;

create table dialogue_phrasing_embedding (
phrasing ulid not null references dialogue_phrasing (id) on delete cascade,
phrasing text not null references dialogue_phrasing (id) on delete cascade,

-- Name of the embedding model
model text not null,
Expand All @@ -132,40 +130,40 @@ create table dialogue_phrasing_embedding (
);

create table chat_reply_edit_log (
id ulid not null default gen_ulid() primary key,
reply_msg ulid not null references chat_message (id) on delete cascade,
id text not null default gen_ulid() primary key,
reply_msg text not null references chat_message (id) on delete cascade,

-- id of the dialogue item added or updated
edited_dialogue ulid not null references dialogue_item (id) on delete cascade
edited_dialogue text not null references dialogue_item (id) on delete cascade
);

-- Metadata for generated reply messages for future analytics.
create table chat_reply_metadata (
reply_msg ulid not null references chat_message (id) on delete cascade primary key,
reply_msg text not null references chat_message (id) on delete cascade primary key,

-- References to dialogue_phrasing
matched ulid[] not null,
matched text[] not null,

match_scores float8[] not null,
best_match_dialogue ulid default null references dialogue_item (id) on delete set null,
best_match_dialogue text default null references dialogue_item (id) on delete set null,

-- The above 3 fields can be empty / null if no good match

-- References to chat_message
model_inputs ulid[] not null,
model_inputs text[] not null,

-- True if we bypassed the LLM due to high confidence
direct_result boolean not null default false,

-- enum: -1 = dislike, 0 = default, 1 = like
user_feedback int not null default 0,

regen_of ulid default null references chat_message (id) on delete set null,
last_edit ulid default null references chat_reply_edit_log (id) on delete set null
regen_of text default null references chat_message (id) on delete set null,
last_edit text default null references chat_reply_edit_log (id) on delete set null
);

create table chat_suggestion (
reply_msg ulid not null references chat_message (id) on delete cascade,
reply_msg text not null references chat_message (id) on delete cascade,
suggestion text not null,

-- enum: 0 = none, 1 = clicked
Expand Down

0 comments on commit 94e6062

Please sign in to comment.