繁体   English   中英

Rails迁移产生错误

[英]Rails migration giving error

我创建了一个称为客户的模型。 迁移文件如下:-

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_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)

卸下,从下面一行:

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

您在第3行的结尾处有一个额外的,

更改此行

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

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

您可以在此处检查语法和其他选项

编辑

对于第二个错误,请从迁移中删除: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

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

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

试试下面

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM