Rails db迁移错误

时间:2018-05-16 07:32:56

标签: ruby-on-rails ruby

我开始在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

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

3 个答案:

答案 0 :(得分:1)

不知道你从哪里获得了积极的记录知识,但是你似乎把它搞砸了。它不是 <?xml version="1.0" ?> <form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd"> <argument name="data" xsi:type="array"> <item name="js_config" xsi:type="array"> <item name="provider" xsi:type="string">chanz_user_form.user_form_data_source</item> <item name="deps" xsi:type="string">chanz_user_form.user_form_data_source</item> </item> <item name="label" translate="true" xsi:type="string">General Information</item> <item name="config" xsi:type="array"> <item name="dataScope" xsi:type="string">data</item> <item name="namespace" xsi:type="string">chanz_user_form</item> </item> <item name="template" xsi:type="string">templates/form/collapsible</item> <item name="buttons" xsi:type="array"> <item name="back" xsi:type="string">Chanz\User\Block\Adminhtml\User\Edit\BackButton</item> <item name="delete" xsi:type="string">Chanz\User\Block\Adminhtml\User\Edit\DeleteButton</item> <item name="save" xsi:type="string">Chanz\User\Block\Adminhtml\User\Edit\SaveButton</item> <item name="save_and_continue" xsi:type="string">Chanz\User\Block\Adminhtml\User\Edit\SaveAndContinueButton</item> </item> </argument> <dataSource name="user_form_data_source"> <argument name="dataProvider" xsi:type="configurableObject"> <argument name="class" xsi:type="string">Chanz\User\Model\User\DataProvider</argument> <argument name="name" xsi:type="string">user_form_data_source</argument> <argument name="primaryFieldName" xsi:type="string">user_id</argument> <argument name="requestFieldName" xsi:type="string">user_id</argument> <argument name="data" xsi:type="array"> <item name="config" xsi:type="array"> <item name="submit_url" path="*/*/save" xsi:type="url"/> </item> </argument> </argument> <argument name="data" xsi:type="array"> <item name="js_config" xsi:type="array"> <item name="component" xsi:type="string">Magento_Ui/js/form/provider</item> </item> </argument> </dataSource> <fieldset name="General"> <argument name="data" xsi:type="array"> <item name="config" xsi:type="array"> <item name="label" xsi:type="string"/> </item> </argument> <argument name="data" xsi:type="array"> <item name="config" xsi:type="array"> <item name="label" xsi:type="string" translate="true">More Product Links</item> <item name="sortOrder" xsi:type="number">1000</item> </item> </argument> <field name="name"> <argument name="data" xsi:type="array"> <item name="config" xsi:type="array"> <item name="dataType" xsi:type="string">text</item> <item name="label" translate="true" xsi:type="string">Name</item> <item name="formElement" xsi:type="string">input</item> <item name="source" xsi:type="string">name</item> <item name="sortOrder" xsi:type="number">10</item> <item name="dataScope" xsi:type="string">name</item> <item name="validation" xsi:type="array"> <item name="required-entry" xsi:type="boolean">true</item> </item> </item> </argument> </field> <field name="dept"> <argument name="data" xsi:type="array"> <item name="config" xsi:type="array"> <item name="dataType" xsi:type="string">text</item> <item name="label" translate="true" xsi:type="string">Department</item> <item name="formElement" xsi:type="string">input</item> <item name="source" xsi:type="string">dept</item> <item name="sortOrder" xsi:type="number">20</item> <item name="dataScope" xsi:type="string">dept</item> <item name="validation" xsi:type="array"> <item name="required-entry" xsi:type="boolean">false</item> </item> </item> </argument> </field> </fieldset> </form> 。它是t.<column> <type>

t.<type> <column>

等等。

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

答案 1 :(得分:0)

它将t.string然后column_name喜欢

def change
    create_table :wa_players, :id => false do |t|
      t.string :id
      t.string :player_name
      t.string :uuid
      t.string :password
      t.string :money
      t.string :items_sold
      t.string :items_bought
      t.string :earnt
      t.string :spent
      t.string :permissions
      t.string :locked
      t.timestamps null: false
    end
    add_index :wa_players, :id, unique: true
end

看看Creating a Table

<强>更新

我不知道您的尝试方法,但请按照以下步骤进行操作

  1. rails g model WaPlayer

  2. 一样运行您的控制台
  3. 转到db/migrate/TIMESTAMP_create_wa_players.rb

  4. 然后将此change方法粘贴到class

  5. 然后在rails db:migrate之后运行rake db:migraterake db:create如果您到目前为止尚未创建数据库

答案 2 :(得分:0)

从提供的内容来看,您似乎走错了方向:

首先,为模型生成迁移,看起来你已经完成了共享模型类。

模型类

class WaPlayer < ApplicationRecord::Base
 has_secure_password
end

因此,会有相同的迁移,即:

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

迁移内容错误,需要更新:

def change
 create_table :wa_players, :id => false do |t|
  t.string :id
  t.string :player_name
  t.string :uuid
  t.string :password
  t.string :money
  t.string :items_sold
  t.string :items_bought
  t.string :earnt
  t.string :spent
  t.string :permissions
  t.string :locked
  t.timestamps null: false
 end
  add_index :wa_players, :id
end

现在你需要运行

rake db:create 

如果尚未迁移数据库,则运行

rake db:migrate.