【发布时间】:2019-05-04 01:54:32
【问题描述】:
我正在尝试完成一个相当简单的壮举,即更改我的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 post 和 this one 似乎有类似的问题。但是,这两个帖子都没有涉及实际答案。有没有人成功摆脱这个?
【问题讨论】:
-
你能显示表
blog的架构吗? -
@devanand 我添加了更多细节和我的架构。你对此有什么理论吗?
-
我知道这与这里无关,但你为什么使用 SQLite ?您真的应该考虑将 PostgreSQL 或 MySQL 用于生产环境 - 甚至是开发环境。
-
@sloneorzeszki SQLite 用于测试和开发环境;在生产环境中使用
gigantic数据库服务器始终是最佳选择——它们也是开源的,不需要许可证。 -
@sloneorzeszki 这个问题的所有问题都直接因为他们使用的是SQLite。我认为有一些非常正当的理由可以避免它。
标签: sql ruby-on-rails sqlite