Qsario
Qsario

Reputation: 1026

Migration fails with Error: NoMethodError: undefined method `Migration'

I'm trying to get set up with Sequel in Ruby. I went to http://sequel.rubyforge.org/rdoc/files/doc/migration_rdoc.html and created my first migration. Then I got Postgres.app running as my server and did a createdb Qsario. The problem comes when I try to use my migration to create the database fields:

$sequel -E -m . postgres://localhost/Qsario
I, [2012-07-08T13:53:49.659795 #6258]  INFO -- : (0.000374s) SET standard_conforming_strings = ON
I, [2012-07-08T13:53:49.660113 #6258]  INFO -- : (0.000153s) SET client_min_messages = 'WARNING'
I, [2012-07-08T13:53:49.660359 #6258]  INFO -- : (0.000163s) SET DateStyle = 'ISO'
I, [2012-07-08T13:53:49.664679 #6258]  INFO -- : (0.000952s) SELECT NULL FROM "schema_info" LIMIT 1
I, [2012-07-08T13:53:49.665179 #6258]  INFO -- : (0.000214s) SELECT * FROM "schema_info" LIMIT 1
I, [2012-07-08T13:53:49.665544 #6258]  INFO -- : (0.000166s) SELECT 1 AS "one" FROM "schema_info" LIMIT 1
I, [2012-07-08T13:53:49.666100 #6258]  INFO -- : (0.000325s) SELECT COUNT(*) AS "count" FROM "schema_info" LIMIT 1
I, [2012-07-08T13:53:49.666461 #6258]  INFO -- : (0.000179s) SELECT "version" FROM "schema_info" LIMIT 1
Error: NoMethodError: undefined method `Migration' for Sequel:Module/Users/me/Projects/Qsario/db/migrate/001_create_user_and_file_tables.rb:3:in `<top (required)>'

Here's what 001_create_user_and_file_tables.rb looks like:

Sequel.Migration do
  no_transaction

  change do
    create_table(:users) do
      primary_key :id
      String      :username, :unique=>true
      String      :email, :unique=>true
      String      :password_hash
      String      :password_salt
      DateTime    :joined_at, :null => false
      FalseClass  :banned, default=>false
      String      :role, default=>"user"
    end

    create_table(:files) do
      primary_key :id
      foreign_key :user_id, :users
      String      :filename, :null => false
      DateTime    :uploaded_at, :null => false
    end

    create_table(:users_files) do
      primary_key :id
      foreign_key :user_id, :users
      foreign_key :file_id, :files
    end
  end
end

Note that there is no Rakefile or anything like that yet because I'm still trying to get things set up. I am not using Rails. So that .rb file is the only thing in the directory.

Upvotes: 0

Views: 1920

Answers (1)

Qsario
Qsario

Reputation: 1026

Just for the record, here's what the fixed migration looks like, with comments around the fixes/improvements in case anyone else makes the same mistake I did. mu is too short deserves all the credit for helping me fix it.

Sequel.migration do  # Lowercase 'm' in migration
  # That no_transaction bit was totally unnecessary.

  change do
    create_table(:users) do
      primary_key :id
      String      :username,    :unique=>true
      String      :email,       :unique=>true
      String      :password_hash
      String      :password_salt
      DateTime    :joined_at,   :null => false
      FalseClass  :banned,      :default=>false  # default needs to be :default
      String      :role,        :default=>"user"
    end

    create_table(:files) do
      primary_key :id
      foreign_key :user_id, :users
      String      :filename,    :null => false
      DateTime    :uploaded_at, :null => false
    end

    # A nicer way to make the old :users_files table.
    create_join_table(:user_id => :users, :file_id => :files)
  end
end

Upvotes: 1

Related Questions