【问题标题】:rails migration to create a table giving "Table doesn't exist" when referencing a modelrails 迁移以创建一个表,在引用模型时给出“表不存在”
【发布时间】:2018-06-03 15:45:05
【问题描述】:

我正在使用带有 mysql2 适配器的 rails 5.1.4。

当我尝试在迁移中创建一个引用另一个表的表时,它给出的表不存在。我不明白为什么会弹出这个错误。看到一个不能帮助解决任何问题的错误是没有意义的。

我阅读了另一篇帖子 (Migration to create table raises Mysql2::Error: Table doesn't exist),提出的解决方案对我有用。我对这个解决方案感到担忧,因为它建议用“整数”替换“引用”并将“_id”添加到被引用的类名中。这使得 DB 不知道 FK 约束(从日志中执行的 mysql 可以看出)。

此外,此错误仅在少数迁移中发生。其他带有引用的迁移工作正常。

如前所述,我认为可行的解决方案并不合适。

失败的迁移代码是这样的:

class CreateLocatableEntitiesPlaceEntitiesPlaces < ActiveRecord::Migration[5.1]
  def change
    create_table :locatable_entities_place_entities_places do |t|
      t.string :name
      t.integer :type
      t.references :locality, foreign_key: true, index: {:name => "index_places_on_locality_id"} 
      t.references :establishment, foreign_key: true, index: {:name => "index_places_on_establishment_id"} 
      t.references :parking, foreign_key: true, index: {:name => "index_places_on_parking_id"} 
      t.boolean :show_in_map
      t.boolean :show_locality_name
      t.date :constructed_on
      t.integer :total_area
      t.float :lat
      t.float :long

    end
  end
end

还想补充一点,我已经在子文件夹中命名了我的模型,这就是我手动命名索引的原因,因为它们变得太大而无法由 MySQL 处理。以防万一它必须用它做任何事情。

以下是我的迁移文件夹的屏幕截图,其中包含所有迁移的运行顺序。

【问题讨论】:

  • 我在尝试为主键为int 的表创建外键时遇到了类似的问题。由于 Rails 5 默认使用 bigint 作为主键和外键,而不是 int,因此我必须在参考中指定 type: :integer。我有点惊讶 Rails 不能自己解决这个问题……

标签: ruby-on-rails rails-migrations


【解决方案1】:

这与迁移文件的顺序有关,在您的 `db/migrate' 文件夹中。

如果migration-file 和包含Foreign Key 的table 在Parent Table 之前执行,则会引发table not found 错误

在您的情况下,应先迁移下表的迁移
index_places_on_locality , index_places_on_establishment, index_places_on_parking

然后 CreateLocatableEntitiesPlaceEntitiesPlaces 表。

在您的 `db/migrate' 文件夹中检查您的订单

迁移文件将以当前日期和时间命名。因此它将按照该顺序执行。参考Running Migrations

【讨论】:

  • 我猜不是这样。请参阅带有我的问题(已编辑)的迁移文件夹的屏幕截图,迁移按时间戳顺序排列。
【解决方案2】:

我意识到我最初的代码出了什么问题。在迁移中将 foreign_key 设置为 true 要求表是可发现的。由于表名与参考文献中指定的名称不明显,因此出现此错误。

在 rails 5+ 中,您可以指定键应该引用的表名。进行此更改后,我能够毫无问题地运行迁移。

以下是更新后的代码:

class CreateLocatableEntitiesPlaceEntitiesPlaces < ActiveRecord::Migration[5.1]
  def change
    create_table :locatable_entities_place_entities_places do |t|
      t.string :name
      t.integer :type
      t.references :locality, foreign_key: {to_table: :base_entities_locality_entities_localities}, index: {:name => "index_places_on_locality_id"}
      t.references :establishment, foreign_key: {to_table: :locatable_entities_place_entities_establishments}, index: {:name => "index_places_on_establishment_id"} 
      t.references :parking, foreign_key: {to_table: :locatable_entities_place_entities_parkings}, index: {:name => "index_places_on_parking_id"} 
      t.boolean :show_in_map
      t.boolean :show_locality_name
      t.date :constructed_on
      t.integer :total_area
      t.float :lat
      t.float :long

    end
  end
end

正如我的问题中提到的,我对模型进行了命名空间,这就是 rails 无法自行找到的表名缺乏明显性的原因。

这篇文章帮助解决了这个问题。:Specifying column name in a "references" migration

【讨论】:

    猜你喜欢
    • 2018-09-22
    • 2018-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多