【问题标题】:Argument Error in migration in Rails projectRails 项目迁移中的参数错误
【发布时间】:2016-03-06 10:46:26
【问题描述】:

我曾使用 sqlite 作为我的 rails 应用程序的数据库,然后我使用 Postgresql 对其进行了更改,之后我在尝试添加新迁移时开始收到错误。

错误

 $ rake db:migrate --trace
 ** Invoke db:migrate (first_time)
 ** Invoke environment (first_time)
 ** Execute environment
 ** Invoke db:load_config (first_time)
 ** Execute db:load_config
 ** Execute db:migrate
   rake aborted!
   ArgumentError: wrong number of arguments (1 for 4..5)
   /Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.4/lib/active_record/connection_adapters/abstract/schema_definitions.rb:99:in `initialize'
   /Library/Ruby/Gems/2.0.0/gems/spatial_adapter-1.2.0/lib/spatial_adapter/postgresql.rb:71:in `new'
   /Library/Ruby/Gems/2.0.0/gems/spatial_adapter-1.2.0/lib/spatial_adapter/postgresql.rb:71:in `create_table'
   /Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.4/lib/active_record/schema_migration.rb:29:in `create_table'
   /Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.4/lib/active_record/connection_adapters/abstract/schema_statements.rb:822:in `initialize_schema_migrations_table'
   /Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.4/lib/active_record/migration.rb:922:in `initialize'
   /Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.4/lib/active_record/migration.rb:820:in `new'
   /Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.4/lib/active_record/migration.rb:820:in `up'
   /Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.4/lib/active_record/migration.rb:798:in `migrate'
   /Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.4/lib/active_record/tasks/database_tasks.rb:137:in `migrate'
   /Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.4/lib/active_record/railties/databases.rake:44:in `block (2 levels) in <top (required)>'
   /Library/Ruby/Gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:240:in `call'
   /Library/Ruby/Gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:240:in `block in execute'
   /Library/Ruby/Gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:235:in `each'
   /Library/Ruby/Gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:235:in `execute'
   /Library/Ruby/Gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:179:in `block in invoke_with_call_chain'
    /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
   /Library/Ruby/Gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:172:in `invoke_with_call_chain'
    /Library/Ruby/Gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:165:in `invoke'
    /Library/Ruby/Gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:150:in `invoke_task'
    /Library/Ruby/Gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:106:in `block (2 levels) in top_level'
    /Library/Ruby/Gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:106:in `each'
    /Library/Ruby/Gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:106:in `block in top_level'
    /Library/Ruby/Gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:115:in `run_with_threads'
    /Library/Ruby/Gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:100:in `top_level'
    /Library/Ruby/Gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:78:in `block in run'
    /Library/Ruby/Gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:176:in   `standard_exception_handling'
    /Library/Ruby/Gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:75:in `run'
    /Library/Ruby/Gems/2.0.0/gems/rake-10.4.2/bin/rake:33:in `<top (required)>'
    /usr/local/bin/rake:23:in `load'
    /usr/local/bin/rake:23:in `<main>'
    Tasks: TOP => db:migrate

_create_users.rb<:migration>

def change
 create_table :users do |t|
  t.string :name
  t.string :email

  t.timestamps null: false
end

_microposts.rb<:migration>

 def change
create_table :microposts do |t|
  t.text :content
  t.references :user, index: true, foreign_key: true

  t.timestamps null: false
end
add_index :microposts, [:user_id, :created_at]
end

AddIndexToUsersEmail<:migration>

  def change
    add_index :users, :email, unique: true
  end

AddPasswordDigestToUsers<:migration>

   def change
      add_column :users, :password_digest, :string
   end

AddRememberDigestToUsers<:migration>

   def change
      add_column :users, :remember_digest, :string
   end

AddAdminToUsers<:migration>

     def change
        add_column :users, :admin, :boolean
     end

AddActivationToUsers<:migration>

     def change
          add_column :users, :activation_digest, :string
          add_column :users, :activated, :boolean,  default: false
          add_column :users, :activated_at, :datetime
     end

创建关系<:migration>

     def change
create_table :relationships do |t|
  t.integer :follower_id
  t.integer :followed_id

  t.timestamps null: false
   end
     add_index :relationships, :follower_id
     add_index :relationships, :followed_id
     add_index :relationships, [:follower_id, :followed_id], unique: true
   end

database.yml

 development:
 adapter: postgresql
 encoding: unicode
 database: blog_development
 pool: 5
 username: ******
 password:  

【问题讨论】:

  • 请添加关于错误的完整报告。您尝试提取的“最小特定部分”不够具体。
  • @D-side 够了吗?如果您需要任何信息,请询问我..
  • 是的,迁移文件似乎也被截断了。
  • @D-side 我已经运行了“rake:db migrate --trace”并添加了所有迁移文件我尝试添加我的数据库..
  • 哦。这可能是问题所在。每个迁移都应该包含在一个继承自 ActiveRecord::Migration 的类中。您是刚刚编辑了它还是它不存在?

标签: ruby-on-rails database rake migrate dbmigrate


【解决方案1】:

从微博中删除 foreign_key: true 并添加外键,如:-

def change
    create_table :microposts do |t|
        t.text :content
        t.references :user, index: true, foreign_key: true
        t.timestamps :null => false
    end
    add_index :microposts, :created_at
end

【讨论】:

  • @user3506853 不幸的是,这是同样的错误。我还尝试了“ rake db:reset ”,我遇到了一个错误 -- enable_extension("plpgsql") -> 0.0445s -- create_table("microposts", {:force=>:cascade}) rake aborted!
  • 我添加了我想要添加我的应用程序的所有迁移文件。我还运行“rake db:migrate --trace”然后我编辑了我的问题。回答我的问题也许对你有用。
猜你喜欢
  • 2016-02-22
  • 1970-01-01
  • 2019-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-17
  • 1970-01-01
  • 2011-07-28
相关资源
最近更新 更多