Column Basics
Columns define the structure of your tables. Every column has a type and optional modifiers that control validation, defaults, and behavior.
Column Types
Section titled “Column Types”Astroladb provides two levels of column types:
- Semantic Types - High-level types with built-in validation (recommended)
- Low-Level Types - Direct SQL type mappings for custom cases
Required vs Optional
Section titled “Required vs Optional”By default, all columns are required (NOT NULL). This is an opinionated design choice that encourages explicit data modeling.
Use .optional() to allow NULL values:
export default table({ id: col.id(), name: col.name(), // Required - NOT NULL bio: col.text().optional(), // Optional - allows NULL website: col.url().optional(), // Optional - allows NULL});Why Required by Default?
Section titled “Why Required by Default?”Required columns prevent common bugs:
- Forced to think about data completeness
- Clearer data contracts
- Fewer defensive null checks in application code
Make fields optional only when NULL has a clear semantic meaning (e.g., “not provided” vs. “empty string”).
Default Values
Section titled “Default Values”Use .default() to set a value for new rows when no value is provided.
export default table({ id: col.id(), title: col.string(200), status: col.string(20).default("draft"),}).timestamps();Unique Constraints
Section titled “Unique Constraints”Use .unique() to ensure no duplicate values exist in a column.
export default table({ id: col.id(), email: col.email().unique(), username: col.string(50).unique(), display_name: col.string(100), // Not unique - duplicates allowed});Combining Modifiers
Section titled “Combining Modifiers”Chain modifiers together for precise column definitions.
export default table({ id: col.id(), website: col.url().optional().unique(), nickname: col.string(30).optional().default("Anonymous"),});