我有一个来自Devise gem的用户类。
我有一个admin_account类,它是从使用STI的user继承的。我有一个admin_task类。我有一个admin_task_assignment类。
我在尝试通过rails控制台创建管理任务分配时遇到外键错误。t = AdminTaskAssignment.create(admin_account_id: 1, admin_task_id: 1)
我同时搜索AdminAccount.find(1)和AdminTask.find(1)来查找存在的记录。但我得到:SQLite3::ConstraintException: FOREIGN KEY constraint failed (ActiveRecord::InvalidForeignKey) FOREIGN KEY constraint failed (SQLite3::ConstraintException)
class AdminAccount < User
has_many :admin_task_assignments, foreign_key: 'admin_account_id'
has_many :admin_tasks, through: :admin_task_assignments
end
class AdminTask < ApplicationRecord
has_many :admin_task_assignments, foreign_key: 'admin_task_id'
has_many :admin_accounts, through: :admin_task_assignments
belongs_to :client
end
class AdminTaskAssignment < ApplicationRecord
belongs_to :admin_account, foreign_key: 'admin_account_id'
belongs_to :admin_task, foreign_key: 'admin_task_id'
end
class Client < ApplicationRecord
has_many :admin_tasks
end
class User < ApplicationRecord
attribute :type, :string
validates :email, uniqueness: true
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
end
create_table "admin_accounts", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "admin_task_assignments", force: :cascade do |t|
t.integer "admin_account_id", null: false
t.integer "admin_task_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["admin_account_id"], name: "index_admin_task_assignments_on_admin_account_id"
t.index ["admin_task_id"], name: "index_admin_task_assignments_on_admin_task_id"
end
create_table "admin_tasks", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "client_id", null: false
t.string "name"
t.string "task_type"
t.index ["client_id"], name: "index_admin_tasks_on_client_id"
end
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "type"
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
add_foreign_key "admin_task_assignments", "admin_accounts"
add_foreign_key "admin_task_assignments", "admin_tasks"
add_foreign_key "admin_tasks", "clients"
1条答案
按热度按时间kcwpcxri1#
首先,从你的模型中删除所有的cruft:
然后,让我们解决数据库模式。
admin_accounts
表。ActiveRecord甚至不会使用它,因为AdminAccount继承自User。请删除它。users
表。如果您还没有合并/推送这些模式更改,纠正这种情况的最简单方法是回滚到创建
admin_accounts
表之前,并修复/删除迁移:然后删除创建
admin_accounts
表的迁移。重新运行迁移,Bob会发现你已经被淘汰了。因为你正在删除表,所以你不必处理一堆带有垃圾数据的孤立记录。如果你需要修复这个问题,你需要删除外键并替换为正确的外键:
请注意,如果您已经向应用添加了一堆数据,则
admin_account_id
列中可能已经有垃圾值,这些值实际上是来自已失效的admin_accounts表的id,而不是用户id。您可能需要截断该表。然后,您可以删除
admin_accounts
表,因为不再有外键引用它: