Skip to content

Column Basics

Columns define the structure of your tables. Every column has a type and optional modifiers that control validation, defaults, and behavior.

Astroladb provides two levels of column types:

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:

schemas/auth/user.js
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
});

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”).

Use .default() to set a value for new rows when no value is provided.

schemas/blog/post.js
export default table({
id: col.id(),
title: col.string(200),
status: col.string(20).default("draft"),
}).timestamps();

Use .unique() to ensure no duplicate values exist in a column.

schemas/auth/account.js
export default table({
id: col.id(),
email: col.email().unique(),
username: col.string(50).unique(),
display_name: col.string(100), // Not unique - duplicates allowed
});

Chain modifiers together for precise column definitions.

schemas/auth/profile.js
export default table({
id: col.id(),
website: col.url().optional().unique(),
nickname: col.string(30).optional().default("Anonymous"),
});