我无法使用db / migrate目录中的两个单独的迁移文件运行rake db:migrate。我正在使用sinatra与activerecord(而不是rails)。
有时间戳的Migration1 :
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
Migration2
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 :
# 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
Rake文件
# 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时,两个命令都会失败。
答案 0 :(得分:2)
在第一次迁移中,请更改此内容:
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|