ruby-on-rails - 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
如果有人知道如何解决,请告诉我如何解决。
解决方案
不知道你从哪里得到你的活动记录知识,但你似乎把它搞砸了。它不是一个t.<column> <type>
. 它是t.<type> <column>
。
t.string :id
t.string :player_name
等等。
(注意:ruby 中的大多数名称都遵循snake_case 命名约定,而不是camelCase)。
推荐阅读
- sql - 如何跨多个表/列查找不同的日期
- python - MongoDB 存储查询和返回值的最佳方式
- php - 提交按钮不起作用。我有一个 mysql 数据库,我正在尝试制作一个 php 表单
- cordova - 如何在 VS2017 Cordova (Windows 10) 中安装模拟器
- javascript - 谷歌地图 API - ReferenceError:谷歌未定义
- python - 如何在 IntelliJ 中打开 .ipynb 文件?
- ajax - 您如何处理 Razor Pages PageRemote 对“加载”数据的验证(例如编辑 ViewModel 页面)?
- php - 如何使用 PHP 下载生成的 pdf 文件?
- python - 如何将 pandas groupby 函数中的数据分配给变量?
- php - 如何将具有相同格式的多个数组与具有相同格式的 1 个数组进行比较?