Skip to content

Instantly share code, notes, and snippets.

@kwiest
Created May 16, 2012 23:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kwiest/2714778 to your computer and use it in GitHub Desktop.
Save kwiest/2714778 to your computer and use it in GitHub Desktop.
Migration example
# Migration (created from Devise)
class DeviseCreateUsers < ActiveRecord::Migration
def self.up
create_table(:users) do |t|
t.database_authenticatable :null => false
t.recoverable
t.rememberable
t.trackable
t.string :name
# t.confirmable
# t.lockable :lock_strategy => :failed_attempts, :unlock_strategy => :both
# t.token_authenticatable
t.timestamps
end
add_index :users, :email, :unique => true
add_index :users, :reset_password_token, :unique => true
# add_index :users, :confirmation_token, :unique => true
# add_index :users, :unlock_token, :unique => true
end
def self.down
drop_table :users
end
end
# db/schema.rb
create_table "users", :id => false, :force => true do |t|
t.integer "id", :null => false
t.string "email", :default => "", :null => false
t.string "encrypted_password", :limit => 128, :default => "", :null => false
t.string "password_salt", :default => "", :null => false
t.string "reset_password_token"
t.string "remember_token"
t.datetime "remember_created_at"
t.integer "sign_in_count", :default => 0
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
# Migration command
rake db:migrate && rake db:test:prepare
# Error received when creating a record
ActiveRecord::StatementInvalid: PG::Error: ERROR: null value in column "id" violates not-null constraint
: INSERT INTO "users" ("created_at", "current_sign_in_at", "current_sign_in_ip", "email", "encrypted_password", "id", "last_sign_in_at", "last_sign_in_ip", "name", "password_salt", "remember_created_at", "remember_token", "reset_password_token", "sign_in_count", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15)
# This happens when running a test
# or when in console with RAILS_ENV=test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment