Rails 可选的外键不起作用
Rails foreign key optional don't work
我的 rails API 模型有问题,我的 plays_cards 模型有一些外键,但有一个我有问题,因为我有 Mysql2::Error: Field 'deck_id' doesn't have a default value
但是我不需要我的套牌和游戏是强制性的。
PS:它在开发中有效但在生产中无效
这是我的模型:
class PlayCard < ApplicationRecord
# @!group RELATIONS
belongs_to :card
belongs_to :deck, optional: true
belongs_to :game, optional: true
belongs_to :user
# @!endgroup
# @!group VALIDATORS
validates :card_id, presence: true, blank: false, nill: false
validates :user_id, presence: true, blank: false, nill: false
validates :atk, presence: true, blank: false, nill: false, numericality: { greater_than_or_equal_to: 0 }
validates :hp, presence: true, blank: false, nill: false, numericality: { greater_than: 0 }
validates :uid, presence: true, allow_blank: false, allow_nil: false, length: { is: 4 }, uniqueness: true
# @!endgroup
end
这是我的迁移:
class CreatePlayCards < ActiveRecord::Migration[5.2]
def change
create_table :play_cards do |t|
t.references :card, foreign_key: true, null: false
t.integer :atk, null: false
t.integer :hp, null: false
t.references :deck, foreign_key: true
t.references :game, foreign_key: true
t.string :uid, limit: 4, null: false
t.timestamps
end
end
end
你有想法吗?
祝你有愉快的一天
t.refrences
行不需要 foreign_key: true
。他们将强制执行参照完整性测试。
回滚,然后将迁移更改为...
t.references :deck
t.references :game
或者你可以只创建整数字段并绕过数据库 integrieites
t.integer :deck_id
t.integer :game_id
试试这个:
has_many :game, optional: true
据我所知,您的实际 Rails 代码没有任何问题;我在本地复制了相关的位和运行,没问题。尽管您在模型中确实有一些拼写错误,但您应该修复(nill 而不是 nil)。我认为这是一个数据库配置问题,特别是考虑到你说它适用于开发而不是生产。谷歌搜索您发布的错误消息 Mysql2::Error: Field 'deck_id' doesn't have a default value
会得到诸如 this 之类的结果。根据其中一个答案:
This is caused by the STRICT_TRANS_TABLES SQL mode defined in the %PROGRAMDATA%\MySQL\MySQL Server 5.6\my.ini
file. Removing that setting and restarting MySQL should fix the problem.
你好,我通过删除我的数据库并重新创建它来解决它...
在 rails 3 上,只需像这样更改您的列的 属性
change_column :table, :column, :integer, null: true
我的 rails API 模型有问题,我的 plays_cards 模型有一些外键,但有一个我有问题,因为我有 Mysql2::Error: Field 'deck_id' doesn't have a default value
但是我不需要我的套牌和游戏是强制性的。
PS:它在开发中有效但在生产中无效
这是我的模型:
class PlayCard < ApplicationRecord
# @!group RELATIONS
belongs_to :card
belongs_to :deck, optional: true
belongs_to :game, optional: true
belongs_to :user
# @!endgroup
# @!group VALIDATORS
validates :card_id, presence: true, blank: false, nill: false
validates :user_id, presence: true, blank: false, nill: false
validates :atk, presence: true, blank: false, nill: false, numericality: { greater_than_or_equal_to: 0 }
validates :hp, presence: true, blank: false, nill: false, numericality: { greater_than: 0 }
validates :uid, presence: true, allow_blank: false, allow_nil: false, length: { is: 4 }, uniqueness: true
# @!endgroup
end
这是我的迁移:
class CreatePlayCards < ActiveRecord::Migration[5.2]
def change
create_table :play_cards do |t|
t.references :card, foreign_key: true, null: false
t.integer :atk, null: false
t.integer :hp, null: false
t.references :deck, foreign_key: true
t.references :game, foreign_key: true
t.string :uid, limit: 4, null: false
t.timestamps
end
end
end
你有想法吗?
祝你有愉快的一天
t.refrences
行不需要 foreign_key: true
。他们将强制执行参照完整性测试。
回滚,然后将迁移更改为...
t.references :deck
t.references :game
或者你可以只创建整数字段并绕过数据库 integrieites
t.integer :deck_id
t.integer :game_id
试试这个:
has_many :game, optional: true
据我所知,您的实际 Rails 代码没有任何问题;我在本地复制了相关的位和运行,没问题。尽管您在模型中确实有一些拼写错误,但您应该修复(nill 而不是 nil)。我认为这是一个数据库配置问题,特别是考虑到你说它适用于开发而不是生产。谷歌搜索您发布的错误消息 Mysql2::Error: Field 'deck_id' doesn't have a default value
会得到诸如 this 之类的结果。根据其中一个答案:
This is caused by the STRICT_TRANS_TABLES SQL mode defined in the
%PROGRAMDATA%\MySQL\MySQL Server 5.6\my.ini
file. Removing that setting and restarting MySQL should fix the problem.
你好,我通过删除我的数据库并重新创建它来解决它...
在 rails 3 上,只需像这样更改您的列的 属性
change_column :table, :column, :integer, null: true