Names
The field name is the TypeScript name.
Where this fits
TypeScript sees the field name. This page is the SQL name, a rename that keeps the old column, and the comment. Visibility decides who can read or write it.
Insert, update, and where use the field name. The SQL name is what the statement quotes.
SQL name
The SQL name is the field name, unless you replace it. Casing "snake" rewrites a name you did not set. publishedAt is published_at. userID is user_i_d. Casing does not rewrite a name you set.
| Call | Result |
|---|---|
.sqlName("headline") | That column is stored as headline. |
sqlName: "note" | The table is stored as note. |
column
.sqlName() replaces one column. Insert still uses the field name.
import { connect } from "okmodel/pg/postgresjs";
import { schema, table, t } from "okmodel/pg";
const notes = table("notes", {
id: t.identity(),
title: t.text().sqlName("headline"),
});
const app = schema({ tables: [notes] });
export async function add(url: string, title: string) {
await using db = connect(url, { schema: app });
return db.notes.insert({ title });
}table
sqlName on the table replaces the table name. The client property stays notes.
import { connect } from "okmodel/pg/postgresjs";
import { schema, table, t } from "okmodel/pg";
const notes = table(
"notes",
{ id: t.identity(), title: t.text() },
{ sqlName: "note" },
);
const app = schema({ tables: [notes] });
export async function add(url: string, title: string) {
await using db = connect(url, { schema: app });
return db.notes.insert({ title });
}OKM1060
An empty column or table sqlName is refused.
OKM1122
A reserved word, a control character, or a name longer than 63 bytes is refused.
OKM1023
Two tables that store the same SQL name are refused. The message lists the table names.
Previous name
A migration plans ALTER TABLE … RENAME TO or RENAME COLUMN. Dropping one name and adding another, with no renamedFrom, is refused and the fix names the line to add. A previous name that the last snapshot does not contain is refused. Remove renamedFrom once the migration that used it has been applied. With no snapshot, that check does not run.
| Call | Result |
|---|---|
.renamedFrom("headline") | The previous column SQL name. |
renamedFrom: "posts" | The previous table SQL name. |
column
.renamedFrom() is the previous column.
import { table, t } from "okmodel/pg";
export const notes = table("notes", {
id: t.identity(),
title: t.text().renamedFrom("headline"),
});table
renamedFrom on the table is the previous table name.
import { table, t } from "okmodel/pg";
export const notes = table(
"notes",
{ id: t.identity(), title: t.text() },
{ renamedFrom: "posts" },
);OKM1060
An empty column or table renamedFrom is refused.
OKM1020
Table renamedFrom … is not in the previous snapshot. A column message names the table and the previous column.
OKM1530
Drop headline and add title could be a rename. The fix is .renamedFrom("headline") on the new column, or renamedFrom: "posts" on the new table.
Comment
.comment() and a table comment are checked. The catalog has no comment field, so neither is emitted. Omit the call to leave it off. An empty table comment is refused. An empty column comment is accepted.
column
.comment() is checked on that column and not stored.
import { table, t } from "okmodel/pg";
export const notes = table("notes", {
id: t.identity(),
title: t.text().comment("Shown in the list"),
});table
A table comment is checked and not stored.
import { table, t } from "okmodel/pg";
export const notes = table(
"notes",
{ id: t.identity(), title: t.text() },
{ comment: "One row per note" },
);OKM1060
An empty table comment is refused.
OKM1122
A comment that contains a control character is refused.