无法创建rake数据库迁移

时间:2015-09-07 12:09:15

标签: ruby activerecord

我正在尝试使用Sinatra为Ruby项目创建迁移。我的模型类位于app.rb文件中。当我运行create_migration时:

rake db:create_migration NAME=create_admins

我通过跟踪获得以下异常:

rake db:create_migration NAME=create_admins --trace
rake aborted!
Don't know how to build task 'db:create_migration'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task_manager.rb:62:in `[]'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:149:in `invoke_task'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:106:in `block (2 levels) in top_level'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:106:in `each'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:106:in `block in top_level'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:115:in `run_with_threads'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:100:in `top_level'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:78:in `block in run'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:176:in `standard_exception_handling'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:75:in `run'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/rake-10.4.2/bin/rake:33:in `<top (required)>'
/usr/local/rvm/gems/ruby-2.2.1/bin/rake:23:in `load'
/usr/local/rvm/gems/ruby-2.2.1/bin/rake:23:in `<main>'
/usr/local/rvm/gems/ruby-2.2.1/bin/ruby_executable_hooks:15:in `eval'
/usr/local/rvm/gems/ruby-2.2.1/bin/ruby_executable_hooks:15:in `<main>'

我的app.rb复制如下:

require 'rubygems'
gem 'activerecord'

require 'sqlite3'
require 'active_record'

ActiveRecord::Base.logger = Logger.new(File.open('database.log', 'w'))
#ActiveRecord::Base.logger = Logger.new(STDERR)

ActiveRecord::Base.establish_connection(
  :adapter => 'sqlite3',
  :host => "localhost",
  :database => 'test6.db'
)

class Bill < ActiveRecord::Base
    belongs_to :admin
end


class Admin < ActiveRecord::Base
    validates :email, presence: true, uniqueness: true
    validates :name, presence: true
    has_many :bills
end

我的Rakefile复制如下:

# Rakefile

require './app'
require 'sinatra'
require 'active_record'
#require 'rake'

注意:我没有使用rails - 我正在使用带有activerecord的sinatra

1 个答案:

答案 0 :(得分:1)

您可以使用sinatra-activerecord代替'activerecord',并进行如下的微小更改 -

Gemfile

中的

source 'https://rubygems.org'
gem "sinatra"
gem "pg"
gem "activerecord"
gem "sinatra-activerecord"
rakefile.rb

中的

require "./app"
require "sinatra/activerecord/rake"

这应该有效