Skip to main content

Proto Options Reference

Import the Ratel proto options:

import "ratelproto/ratelproto.proto";

ratel.table (Message Option)

Applied to a protobuf message to configure table generation.

message User {
option (ratel.table) = {
generate: true
table_name: "users"
schema: "store"
indexes: [...]
unique: [...]
primary_key: { columns: [...] }
constraints: [...]
post_statements: [...]
};
}
FieldTypeDefaultDescription
generateboolfalseEnable code generation
table_namestringsnake_case(MessageName)SQL table name
schemastring"" (public)PostgreSQL schema
virtual_columnsrepeated VirtualColumn[]Columns without proto field
indexesrepeated Index[]Table indexes
uniquerepeated UniqueColumns[]Composite unique constraints
primary_keyPrimaryKeyColumns-Composite primary key
constraintsrepeated string[]Table-level CHECK constraints
post_statementsrepeated string[]Raw SQL after CREATE TABLE

Index

FieldTypeDescription
namestringIndex name (auto-generated if empty)
columnsrepeated stringColumn names
uniqueboolUNIQUE index
wherestringPartial index WHERE clause
usingstringIndex method (btree, hash, gin, gist, brin)
includerepeated stringINCLUDE columns (covering index)
expressionsrepeated stringExpression-based index
concurrentboolCREATE INDEX CONCURRENTLY

UniqueColumns

FieldTypeDescription
columnsrepeated stringColumn names for composite unique

PrimaryKeyColumns

FieldTypeDescription
columnsrepeated stringColumn names for composite PK

VirtualColumn

Columns that exist in the database but have no corresponding proto field. Ratel automatically adds them to the Scanner struct, DDL, column constants, and query accessors.

FieldTypeDescription
sql_namestringColumn name in SQL
sql_typestringSQL type (e.g., TEXT, BIGINT, TIMESTAMPTZ)
constraintsConstraintColumn constraints (default, check, references, etc.)
is_nullableboolWhether column allows NULL
is_arrayboolWhether column is an array type
message User {
option (ratel.table) = {
generate: true
table_name: "users"
virtual_columns: [
{ sql_name: "password_hash", sql_type: "TEXT" },
{ sql_name: "db_created_at", sql_type: "TIMESTAMPTZ", constraints: { default_value: "now()" } }
]
};

int64 id = 1;
string email = 2;
}

Generated code includes:

  • Scanner field: PasswordHash string, DbCreatedAt string
  • Chainable setter: scanner.WithPasswordHash("hash")
  • Column constant: UserColumnPasswordHash
  • Typed accessor: Users.PasswordHash (TextColumnI)
  • DDL column in CREATE TABLE
  • Included in AllSetters(), GetTarget(), GetSetter(), GetValue()
  • Skipped in IntoPlain() / IntoPb() (no proto field)

additional_code (File Option)

File-level SQL statements for extensions, functions, and other DDL not tied to a specific table.

import "ratelproto/ratelproto.proto";

option (ratel.additional_code) = "CREATE EXTENSION IF NOT EXISTS pg_trgm";
option (ratel.additional_code) = "CREATE OR REPLACE FUNCTION set_updated_at() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN NEW.updated_at = now(); RETURN NEW; END; $$";

Generates var AdditionalSQL []ddl.SchemaSqler in the _ratel.pb.go file. Pass these to ddl.SchemaSQL() alongside table definitions.

ratel.column (Field Option)

Applied to a protobuf field to configure column generation.

int64 id = 1 [(ratel.column) = {
constraints: { primary_key: true }
skip: false
}];
FieldTypeDefaultDescription
constraintsConstraint-Column constraints
skipboolfalseSkip this field in DDL

Constraint

FieldTypeDefaultDescription
uniqueboolfalseUNIQUE constraint
primary_keyboolfalsePRIMARY KEY
default_valuestring""DEFAULT expression
rawstring""Raw constraint SQL
checkstring""CHECK expression
references_tablestring""FK target table
references_columnstring""FK target column
references_schemastring""FK target schema
on_deleteReferenceActionNO_ACTIONON DELETE action
on_updateReferenceActionNO_ACTIONON UPDATE action

ReferenceAction

enum ReferenceAction {
NO_ACTION = 0;
CASCADE = 1;
SET_NULL = 2;
SET_DEFAULT = 3;
RESTRICT = 4;
}

ratel.relation (Field Option)

Applied to a protobuf field to define a relation.

repeated Order orders = 10 [(ratel.relation) = {
one_to_many: { ref_name: "user_id" }
}];

OneToMany / HasOne

FieldTypeDescription
ref_namestringFK column name in child table
on_delete_cascadeboolAdd ON DELETE CASCADE
on_delete_set_nullboolAdd ON DELETE SET NULL
existed_fieldboolFK field exists in child message

BelongsTo

FieldTypeDescription
foreign_keystringFK column in current table
owner_keystringPK in parent table (default: id)

ManyToMany

FieldTypeDescription
ref_on_delete_cascadeboolCASCADE on FK to this table
back_ref_on_delete_cascadeboolCASCADE on FK to related table
pivot_tablePivotTableCustom pivot table config