首页 > 解决方案 > Rails db:当外键很大时,seed 不会创建记录

问题描述

我正在尝试填充 mysql2 数据库,但并非所有记录都添加到数据库中。

这是我的架构

create_table "accounts", id: :integer, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
  t.string "name_es"
  t.string "name_en"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
  t.integer "dbid"  

create_table "company_account_categories", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
  t.integer "company_type"
  t.integer "account_id"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
  t.index ["account_id"], name: "index_company_account_categories_on_account_id"

add_foreign_key "company_account_categories", "accounts"

在我的种子中,我的 CompanyAccountCategory 记录的 account_id 介于 101 和 177 之间,这些记录都被写入数据库,而 account_id 为 22077 或更大的记录则没有。

例子:

CompanyAccountCategory.find_or_create_by(account_id: 176, company_type: 4)
CompanyAccountCategory.find_or_create_by(account_id: 22133, company_type: 1)

标签: ruby-on-rails

解决方案


推荐阅读