新手问题 跪求各位大神指点关于 rails rake db:migrate 迁移的问题

ghn645568344 · 2015年09月18日 · 最后由 peter 回复于 2015年09月18日 · 3123 次阅读

运行 bin/rake db:migrate 时报

-- create_table(:btpzs)
rake aborted!
StandardError: An error has occurred, all later migrations canceled:

undefined method `int' for #<ActiveRecord::ConnectionAdapters::TableDefinition:0x007f24c63b6900>/root/haonan_file/dvd/bpdvds/bp/db/migrate/20150918095902_create_btpzs.rb:4:in `block in change'
/root/haonan_file/dvd/bpdvds/bp/db/migrate/20150918095902_create_btpzs.rb:3:in `change'
-e:1:in `<main>'
NoMethodError: undefined method `int' for #<ActiveRecord::ConnectionAdapters::TableDefinition:0x007f24c63b6900>
/root/haonan_file/dvd/bpdvds/bp/db/migrate/20150918095902_create_btpzs.rb:4:in `block in change'
/root/haonan_file/dvd/bpdvds/bp/db/migrate/20150918095902_create_btpzs.rb:3:in `change'
-e:1:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

这个代码

class CreateBtpzs < ActiveRecord::Migration
  def change
    create_table :btpzs do |t|
      t.int :id
      t.string :ids
      t.string :dvd_name
      t.string :new_data
      t.text :jianjie
      t.string :img_min
      t.text :img
      t.string :url

      t.timestamps null: false
    end
  end
end

当我把 int 改成 integer 的时候 又会报

== 20150918095902 CreateBtpzs: migrating ======================================
-- create_table(:btpzs)
rake aborted!
StandardError: An error has occurred, all later migrations canceled:

you can't redefine the primary key column 'id'. To define a custom primary key, pass { id: false } to create_table./root/haonan_file/dvd/bpdvds/bp/db/migrate/20150918095902_create_btpzs.rb:4:in `block in change'
/root/haonan_file/dvd/bpdvds/bp/db/migrate/20150918095902_create_btpzs.rb:3:in `change'
-e:1:in `<main>'
ArgumentError: you can't redefine the primary key column 'id'. To define a custom primary key, pass { id: false } to create_table.
/root/haonan_file/dvd/bpdvds/bp/db/migrate/20150918095902_create_btpzs.rb:4:in `block in change'
/root/haonan_file/dvd/bpdvds/bp/db/migrate/20150918095902_create_btpzs.rb:3:in `change'
-e:1:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
root@iZ28zli8s44Z:~/haonan_file/dvd/bpdvds/bp# sudo svn up
Updating '.':
U    db/migrate/20150918095902_create_btpzs.rb
Updated to revision 213320.

t.int :id 不需要定义。

you can't redefine the primary key column 'id'. To define a custom primary key, pass { id: false } to create_table.

会与默认的冲突

去掉 id 那一行就 ok 了

@ghn645568344

you can't redefine the primary key column 'id'.

另外请楼主不要跪求,这不是 ruby 社区该出现的词

需要 登录 后方可回复, 如果你还没有账号请 注册新账号