【问题标题】:How to solve: SQLException: no such table: locations: ALTER TABLE "locations" ADD "user_id" integer如何解决: SQLException: no such table: locations: ALTER TABLE "locations" ADD "user_id" integer
【发布时间】:2019-09-17 06:46:56
【问题描述】:

我在使用 rails 时遇到了一些数据库问题。当我运行rails db:migrate 时,出现以下错误:

add_column(:locations, :user_id, :integer)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

SQLite3::SQLException: no such table: locations: ALTER TABLE "locations" ADD "user_id" integer

问题似乎是我有一个迁移尝试将:user_id 添加到表:locations 但该表不存在,因为我删除了它,因此它不在架构中。我该如何解决这个问题?我一直认为删除迁移是个坏主意。

【问题讨论】:

    标签: ruby-on-rails ruby postgresql sqlite


    【解决方案1】:

    这取决于。 locations 是由迁移创建和删除的吗?如果是这样,在时间上,这些迁移之间的迁移失败了吗?如果答案是,那么你不应该删除它。

    另一方面,如果您手动删除了locations,或者在迁移失败之前删除了locations,则将其删除,因为这对您的架构没有意义。

    【讨论】:

    • locations 表是由rails g model Location 创建的,但似乎没有它的迁移文件。当我试图从模式中删除表时,也许我以某种方式删除了它。如果我删除迁移,我只是手动删除文件吗?或者有什么我应该使用的命令?
    • 删除文件即可。
    猜你喜欢
    • 2014-11-04
    • 2012-05-15
    • 1970-01-01
    • 2016-08-09
    • 1970-01-01
    • 1970-01-01
    • 2011-02-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多