Skip to content

Creating table in model

leucos edited this page Oct 14, 2012 · 2 revisions

If you need a to create a table for a model but don't want to deal with migration, you can create table directly in models by using the Sequel schema plugin.

Here is an example, for a quick and dirty users table :

class User < Sequel::Model
  # The schema plugin is required to have access to set_schema
  plugin :schema

  # Set schema for this model
  set_schema do
    primary_key :id

    varchar :email, :unique => true, :empty => false
    varchar :password, :empty => false
    boolean :super_powers, :default => false
  end

  # Create the database table if it doesn't exists
  # Also add a user
  if ! table_exists?
    create_table
    create :email => 'admin', :password => '1234', :super_powers => true
  end
end

Two remarks here :