【问题标题】:Unable to run rake db:migrate using ruby, sinatra and activerecord无法运行 rake db:migrate using ruby​​、sinatra 和 activerecord
【发布时间】:2015-12-03 15:01:09
【问题描述】:

我无法使用 db/migrate 目录中的两个单独的迁移文件运行 rake db:migrate。我正在使用带有 activerecord 的 sinatra(不是 rails)。

带有时间戳的迁移1

class CreateAdmins < ActiveRecord::Migration
  #def change
  #end
  def up
     create_table :admins do |t|
        t.string :email
        t.string :name
     end
  end

  def down
      drop table :admins
  end
end

迁移2

    class CreateBills < ActiveRecord::Migration
      #def change
      #end
      def up
         create_table: bills do |t|
             t.string :email
             t.string :title
             t.text :body
         end
      end

  def down
    drop_table :bills
   end
end

宝石文件

# Gemfile

source 'https://rubygems.org'


gem "sinatra"
gem "activerecord"
gem "sinatra-activerecord"
gem 'sinatra-flash'
gem 'sinatra-redirect-with-flash'
gem 'rake'

group :development do
 gem 'sqlite3'
 gem "tux"
end

group :production do
 gem 'sqlite3'
end

Rakefile

# Rakefile

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

当我运行 rake db:migrate 时,我收到下面复制的错误。

rake aborted!
Errno::ENOENT: No such file or directory @ rb_sysopen - db:migrate
/opt/test/ruby/6/app.rb:31:in `gets'
/opt/test/ruby/6/app.rb:31:in `gets'
/opt/test/ruby/6/app.rb:31:in `<top (required)>'
/opt/test/ruby/6/Rakefile:3:in `require'
/opt/test/ruby/6/Rakefile:3:in `<top (required)>'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/rake_module.rb:28:in `load'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/rake_module.rb:28:in `load_rakefile'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:689:in `raw_load_rakefile'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:94:in `block in load_rakefile'
/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:93:in `load_rakefile'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:77: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 文件的一部分,它允许我运行 rake db:migrate。

 rake db:migrate --trace
** Invoke db:migrate (first_time)
** Invoke db:environment (first_time)
** Execute db:environment
** Invoke db:load_config (first_time)
** Execute db:load_config
** Execute db:migrate
** Invoke db:_dump (first_time)
** Execute db:_dump
** Invoke db:schema:dump (first_time)
** Invoke db:environment
** Invoke db:load_config
** Execute db:schema:dump

但是,即使在运行 rake db:migrate 之后,我也无法在数据库中找到表(使用 sqlite)。我尝试通过我的 ruby​​ 文件添加管理模型,但出现以下错误:

/usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.4/lib/active_record/connection_adapters/sqlite3_adapter.rb:511:in `table_structure': Could not find table 'admins' (ActiveRecord::StatementInvalid)
        from /usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.4/lib/active_record/connection_adapters/sqlite3_adapter.rb:385:in `columns'
        from /usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.4/lib/active_record/connection_adapters/schema_cache.rb:43:in `columns'
        from /usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.4/lib/active_record/attributes.rb:93:in `columns'
        from /usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.4/lib/active_record/attributes.rb:98:in `columns_hash'
        from /usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.4/lib/active_record/inheritance.rb:205:in `subclass_from_attributes?'
        from /usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.4/lib/active_record/inheritance.rb:54:in `new'
        from /usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.4/lib/active_record/persistence.rb:50:in `create!'

ruby 版本是 2.2.1p85

当我尝试运行 rake db:rollback 时,它无法抱怨要删除的表不存在这一事实。

我可以看到 rake db:migrate 后 schema.rb 文件没有更新

ActiveRecord::Schema.define(version: 20150907140840) do

end  

当我尝试运行 rake db:create 或 rake db:setup 时,两个命令都失败了。

【问题讨论】:

  • 我忘了注释掉更改部分。
  • @user3813256 你解决过这个问题吗?

标签: ruby activerecord


【解决方案1】:

在您的第一次迁移中,更改以下内容:

  def up
     create_table :admins do |t|
        t.string = email
        t.string = name
     end
  end

收件人:

  def up
     create_table :admins do |t|
        t.string :email
        t.string :name
     end
  end

另一个问题是,在您的第二次迁移中,create_table: bills do |t| 应该是:create_table :bills do |t|

【讨论】:

  • 我认为这是下一个错误。如果到达该代码,我希望看到 NoMethodError,但看起来 OP 甚至无法让 db:migrate 运行。
  • 修复第一次迁移后,我仍然遇到同样的错误。
  • 嗯,是的,因为 mu 太短了,这是第二个问题。你还有其他事情要发生。
  • 另一个问题是,在您的第二次迁移中:create_table: bills do |t| 应该是:create_table :bills do |t|
  • 修复此问题并重试?让我知道。
猜你喜欢
  • 2015-12-03
  • 2011-10-31
  • 2017-07-30
  • 1970-01-01
  • 1970-01-01
  • 2016-03-08
  • 1970-01-01
  • 2013-05-11
相关资源
最近更新 更多