【发布时间】:2016-08-29 07:06:34
【问题描述】:
我正在尝试在我的 Heroku 服务器上为我的 Rails 项目播种我的 MySQL 数据库,但出现此错误:
ActiveRecord::InvalidForeignKey: Mysql2::Error: 无法添加或更新子行:外键约束失败 (
heroku_b08bb4ad8dfb726.posts, CONSTRAINTfk_rails_5b5ddfd518FOREIGN KEY (user_id) 参考users(id)): INSERT INTOposts(id,title,description,user_id,category_id,created_at,updated_at, '标题' Desc', 1, 1, '2016-08-29 06:53:04', '2016-08-29 06:53:04')
令人惊讶的是,我的开发环境中没有出现此错误。
我的种子文件的摘录如下所示:
# Creating Users here
Category.create!([
{ id: 1, name: "Category"},
])
Post.create!([
{ id: 1, title: "Title", description: "Desc", user_id: "1", category_id: "1" },
])
后模型:
class Post < ApplicationRecord
belongs_to :user
belongs_to :category
has_many :comments
validates :title, :description, presence: true
end
类别模型:
class Category < ApplicationRecord
has_many :posts
end
如果您在 cmets 中需要更多代码 sn-ps,请告诉我。非常感谢您对此问题的任何想法。
【问题讨论】:
标签: mysql ruby-on-rails heroku