gpt4 book ai didi

sql - Rails 5.2 更改或删除表列时出错(SQLite3::ConstraintException:FOREIGN KEY 约束失败:DROP TABLE)

转载 作者:IT王子 更新时间:2023-10-29 06:22:47 26 4
gpt4 key购买 nike

我正在尝试完成一项相当简单的壮举,即更改我的 Blog 表中其中一列的默认值。我有以下迁移:

class UpdateBlogFields < ActiveRecord::Migration[5.2]
def change
change_column :blogs, :freebie_type, :string, default: "None"
end
end

相当简单,但是当我运行 rake db:migrate 时出现以下错误:

StandardError: An error has occurred, this and all later migrations canceled:
SQLite3::ConstraintException: FOREIGN KEY constraint failed: DROP TABLE "blogs"

每次尝试更改或删除列时都会出现此错误,但在添加列时却不会。

我的架构如下所示:

  create_table "blogs", force: :cascade do |t|
t.string "title"
t.string "teaser"
t.text "body"
t.string "category", default: "General"
t.string "linked_module"
t.boolean "published", default: false
t.datetime "published_on"
t.integer "user_id"
t.integer "image_id"
t.integer "pdf_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "slug"
t.string "cta_read_more", default: "Read More"
t.string "cta_pdf", default: "Get My Free PDF"
t.string "cta_video", default: "Watch the Video"
t.string "convertkit_data_form_toggle"
t.string "convertkit_href"
t.integer "pin_image_id"
t.string "data_pin_description"
t.string "freebie_filename"
t.string "video_link"
t.string "freebie_type", default: "File"
t.string "freebie_description"
t.integer "comments_count"
t.integer "subcategory_id"
t.boolean "affiliate_links", default: true
t.boolean "approved", default: false
t.boolean "submitted", default: false
t.index ["image_id"], name: "index_blogs_on_image_id"
t.index ["pdf_id"], name: "index_blogs_on_pdf_id"
t.index ["pin_image_id"], name: "index_blogs_on_pin_image_id"
t.index ["slug"], name: "index_blogs_on_slug", unique: true
t.index ["subcategory_id"], name: "index_blogs_on_subcategory_id"
t.index ["user_id"], name: "index_blogs_on_user_id"
end

这似乎是 SQLite 的东西,因为 this postthis one似乎有类似的问题。但是,这两个帖子都不涉及实际答案。有没有人成功摆脱这个?

最佳答案

更新:

可以通过 Rails 添加新列默认值,而无需使用数据库。在Blog模型中,我们可以使用ActiveRecord::Attributes::ClassMethods::attribute重新定义 freebie_type 的默认值:

属性:freebie_type,:string,默认值:'None'

这将更改业务逻辑级别的默认设置。因此,它依赖于使用 ActiveRecord 才能被识别。通过 SQL 操作数据库仍将使用旧的默认值。要在所有情况下更新默认值,请参阅下面的原始答案。

原始答案:

不幸的是,ALTER COLUMN 只是 minimally supported通过 SQLite。解决方法是创建一个新表,将信息复制到其中,删除旧表,最后重命名新表。这就是 Rails 试图做的,但没有首先禁用外键约束。 user_idimage_idpdf_id 的外键关系正在阻止表删除。

您需要手动进行更新,使用 SQL(首选)或 ActiveRecord::Base.connection。你可以看到 process here在“修改表中的列”下。您可以在 SQLite Create Table Documentation 中找到可用于列的所有选项。 .

PRAGMA foreign_keys=off;

BEGIN TRANSACTION;

ALTER TABLE table1 RENAME TO _table1_old;

CREATE TABLE table1 (
( column1 datatype [ NULL | NOT NULL ] DEFAULT (<MY_VALUE>),
column2 datatype [ NULL | NOT NULL ] DEFAULT (<MY_VALUE>),
...
);

INSERT INTO table1 (column1, column2, ... column_n)
SELECT column1, column2, ... column_n
FROM _table1_old;

COMMIT;

PRAGMA foreign_keys=on;

请确保所有列都按照您想要的方式设置,因为您将无法在创建表后修复它!展望 future ,我强烈建议设置 PostgreSQL 或 MySQL2 数据库。它们更强大,也更容易修改和维护。

关于sql - Rails 5.2 更改或删除表列时出错(SQLite3::ConstraintException:FOREIGN KEY 约束失败:DROP TABLE),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53582444/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com