Rails迁移给出错误

时间:2017-04-28 10:19:12

标签: ruby-on-rails database-migration rails-migrations

我创建了一个名为customer的模型。迁移文件如下: -

class CreateCustomers < ActiveRecord::Migration[5.0]
  def change
        create_table :customers , :primary_key => :customer_id , do |t|
          t.integer :customer_id
          t.string :first_name
          t.string :last_name
          t.string :address_1
          t.string :address_2
          t.string :city
          t.string :state
          t.bigint :postal_code

          t.timestamps
        end
  end
end

现在当我运行rails db:migrate时,这是我得到的错误: -

rails aborted!
SyntaxError: /home/gauri/Academics/CS/Ruby on Rails/ScholarShip/ScholarShip/db/migrate/20170428100848_create_customers.rb:3: syntax error, unexpected keyword_do_block
imary_key => :customer_id , do |t|
                              ^
/home/gauri/Academics/CS/Ruby on Rails/ScholarShip/ScholarShip/db/migrate/20170428100848_create_customers.rb:4: syntax error, unexpected tSYMBEG, expecting keyword_end
          t.integer :customer_id
                     ^
/home/gauri/Academics/CS/Ruby on Rails/ScholarShip/ScholarShip/db/migrate/20170428100848_create_customers.rb:16: syntax error, unexpected keyword_end, expecting end-of-input
/home/gauri/Academics/CS/Ruby on Rails/ScholarShip/ScholarShip/bin/rails:9:in `require'
/home/gauri/Academics/CS/Ruby on Rails/ScholarShip/ScholarShip/bin/rails:9:in `<top (required)>'
/home/gauri/Academics/CS/Ruby on Rails/ScholarShip/ScholarShip/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

schema.rb文件也没有customer表的迹象。有人可以帮忙吗? 编辑: 在customer_id之后删除逗号之后我得到了这些: -

== 20170428100848 CreateCustomers: migrating ==================================
-- create_table(:customers, {:primary_key=>:customer_id})
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:

you can't redefine the primary key column 'customer_id'. To define a custom primary key, pass { id: false } to create_table.
/home/gauri/Academics/CS/Ruby on Rails/ScholarShip/ScholarShip/db/migrate/20170428100848_create_customers.rb:4:in `block in change'
/home/gauri/Academics/CS/Ruby on Rails/ScholarShip/ScholarShip/db/migrate/20170428100848_create_customers.rb:3:in `change'
/home/gauri/Academics/CS/Ruby on Rails/ScholarShip/ScholarShip/bin/rails:9:in `require'
/home/gauri/Academics/CS/Ruby on Rails/ScholarShip/ScholarShip/bin/rails:9:in `<top (required)>'
/home/gauri/Academics/CS/Ruby on Rails/ScholarShip/ScholarShip/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'
ArgumentError: you can't redefine the primary key column 'customer_id'. To define a custom primary key, pass { id: false } to create_table.
/home/gauri/Academics/CS/Ruby on Rails/ScholarShip/ScholarShip/db/migrate/20170428100848_create_customers.rb:4:in `block in change'
/home/gauri/Academics/CS/Ruby on Rails/ScholarShip/ScholarShip/db/migrate/20170428100848_create_customers.rb:3:in `change'
/home/gauri/Academics/CS/Ruby on Rails/ScholarShip/ScholarShip/bin/rails:9:in `require'
/home/gauri/Academics/CS/Ruby on Rails/ScholarShip/ScholarShip/bin/rails:9:in `<top (required)>'
/home/gauri/Academics/CS/Ruby on Rails/ScholarShip/ScholarShip/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

4 个答案:

答案 0 :(得分:1)

从下面的行中删除,

create_table :customers , :primary_key => :customer_id , do |t|

正确的是:

create_table :customers , :primary_key => :customer_id do |t|

设置主键的代码:

 create_table(:my_table, :primary_key => 'userID') do |t|
   # Primary key column will be created automatically
   # Do not create here
   # t.column :userID, :integer, :null => false
   ...
 end

答案 1 :(得分:0)

你在行尾有一个额外的,:3

更改此行

create_table :customers , :primary_key => :customer_id , do |t|

create_table :customers , :primary_key => :customer_id do |t|

您可以检查语法和其他选项here

修改

对于第二个错误,请从迁移中删除:primary_key => :customer_id

class CreateCustomers < ActiveRecord::Migration[5.0]
  def change
    create_table :customers, id: false do |t|
      t.integer :customer_id
      t.string :first_name
      t.string :last_name
      t.string :address_1
      t.string :address_2
      t.string :city
      t.string :state
      t.bigint :postal_code

      t.timestamps
    end
  end
end

并在模型中指定主键

class Customer
  self.primary_key = :customer_id
end

答案 2 :(得分:0)

删除第三行的最后一个逗号:

create_table :customers , :primary_key => :customer_id do |t|

答案 3 :(得分:0)

尝试以下

class CreateCustomers < ActiveRecord::Migration[5.0]
  def change
        create_table :customers , :id => :false do |t|
          t.integer :customer_id, primary: true
          t.string :first_name
          t.string :last_name
          t.string :address_1
          t.string :address_2
          t.string :city
          t.string :state
          t.bigint :postal_code

          t.timestamps
        end
  end
end