首页 > 解决方案 > Rails db 迁移错误

问题描述

我开始在rails中编写一个api。我想将我的模型迁移到 db,但出现此错误:

E:\WebAuction\Backend\api>rails db:migrate
rails db:migrate
rails 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.
E:/WebAuction/Backend/api/db/migrate/20180516070242_create_wa_players.rb:4:in `block in change'
E:/WebAuction/Backend/api/db/migrate/20180516070242_create_wa_players.rb:3:in `change'
bin/rails:4:in `<main>'

Caused by:
ArgumentError: you can't redefine the primary key column 'id'. To define a custom primary key, pass { id: false } to create_table.
E:/WebAuction/Backend/api/db/migrate/20180516070242_create_wa_players.rb:4:in `block in change'
E:/WebAuction/Backend/api/db/migrate/20180516070242_create_wa_players.rb:3:in `change'
bin/rails:4:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
== 20180516070242 CreateWaPlayers: migrating ==================================
-- create_table(:wa_players)

我的模型课

class WaPlayer < ApplicationRecord::Base
  has_secure_password

  def change
    create_table :wa_players, :id => false do |t|
      t.id :String
      t.playerName :String
      t.uuid :String
      t.password :String
      t.money :String
      t.itemsSold :String
      t.itemsBought :String
      t.earnt :String
      t.spent :String
      t.Permissions :String
      t.Locked :String
      t.timestamps null: false
    end
    add_index :wa_players, :id
  end
end

如果有人知道如何解决,请告诉我如何解决。

标签: ruby-on-railsruby

解决方案


不知道你从哪里得到你的活动记录知识,但你似乎把它搞砸了。它不是一个t.<column> <type>. 它是t.<type> <column>

t.string :id
t.string :player_name

等等。

(注意:ruby 中的大多数名称都遵循snake_case 命名约定,而不是camelCase)。


推荐阅读