Generic array column with custom element type
Options
- element_type: Type of array elements (default: :text)
- null: Allow NULL values (default: true)
- default: Default value
Example
create_table :data do |t|
t.array :values, element_type: :float, null: false
end
Sourcebigint_primary_key(name :
String = "id", default :
String |
Nil = nil)
Bigint primary key (non-auto-incrementing)
Creates a BIGINT primary key without auto-increment. Useful when you need
to control ID generation (e.g., distributed IDs, snowflake IDs).
For auto-incrementing integer primary keys, use primary_key() instead.
Options
- name: Column name (default: "id")
- default: SQL default expression
Example
create_table :events do |t|
t.bigint_primary_key # Application generates IDs
t.string :type
end
SourceEnum column - stores enum values in the database
Options
- values: Array of allowed string values (required)
- storage: Storage strategy - :string (default), :integer, or :native (PostgreSQL only)
- null: Allow NULL values (default: true)
- default: Default value
Example
create_table :users do |t|
t.enum :status, values: ["active", "inactive", "suspended"], null: false
t.enum :priority, values: ["low", "medium", "high"], storage: :integer
end
Sourcefull_text_index(column :
String, config :
String = "english", name :
String |
Nil = nil, fastupdate :
Bool = true)
Add a full-text search index (GIN on tsvector)
Creates a GIN index on a tsvector expression for efficient full-text search.
Example
create_table :articles do |t|
t.string :title
t.text :content
# Single column
t.full_text_index :title
# Multiple columns
t.full_text_index [:title, :content], config: "english"
end
Options
- config: Text search configuration (default: "english")
- name: Custom index name
- fastupdate: Use fast update optimization (default: true)
SourceAdd a full-text search index on multiple columns
Sourcepostgres_indexes
Get all PostgreSQL-specific indexes
SourceType-aware primary key
Creates a primary key column with the specified type. Useful for UUID,
string, or other non-integer primary keys.
Options
- type: Column type (:uuid, :string, :text, :integer, :bigint)
- default: SQL default expression (e.g., "gen_random_uuid()" for PostgreSQL UUID)
Example
create_table :users do |t|
t.primary_key "id", :uuid # UUID primary key
t.string :name
end
create_table :settings do |t|
t.primary_key "key", :string # String primary key
t.text :value
end
Backend Behavior
| Type | SQLite | PostgreSQL |
|------|--------|------------|
| :uuid | CHAR(36) PRIMARY KEY NOT NULL | UUID PRIMARY KEY NOT NULL DEFAULT gen_random_uuid() |
| :string | TEXT PRIMARY KEY NOT NULL | TEXT PRIMARY KEY NOT NULL |
| :integer | INTEGER PRIMARY KEY AUTOINCREMENT | SERIAL PRIMARY KEY |
| :bigint | INTEGER PRIMARY KEY AUTOINCREMENT | BIGSERIAL PRIMARY KEY |
Sourcereference(name :
String, polymorphic :
Bool = false, null :
Bool = true, foreign_key :
String |
Nil = nil, to_table :
String |
Nil = nil, on_delete :
Symbol |
Nil = nil, on_update :
Symbol |
Nil = nil, index :
Bool = true)
Reference column (foreign key column)
Creates a foreign key column pointing to another table.
Options
- polymorphic: If true, creates both
{name}_id and {name}_type columns
- null: Allow NULL values (default: true)
- foreign_key: Custom column name for the FK (default:
{name}_id)
- to_table: Target table name (default: pluralized
name)
- on_delete: Action on delete - :cascade, :nullify, :restrict, :no_action (default: nil)
- on_update: Action on update - :cascade, :nullify, :restrict, :no_action (default: nil)
- index: Add an index on the FK column (default: true)
Example
create_table :posts do |t|
t.reference :user, null: false, on_delete: :cascade
t.reference :author, to_table: :users, foreign_key: "author_id"
t.reference :commentable, polymorphic: true
end
Sourcesoft_deletes
Add deleted_at timestamp column for soft deletes
Use this in conjunction with the paranoid macro in your model
to enable soft delete functionality.
Example
# Migration
create_table :users do |t|
t.primary_key
t.string :name
t.timestamps
t.soft_deletes # adds deleted_at column
end
# Model
class User < Ralph::Model
table :users
column id, Int64, primary: true
column name, String
timestamps
paranoid # enables soft delete behavior
end
SourceText column (unlimited length string)
Options
- null: Allow NULL values (default: true)
- default: Default value
Source