关系不存在问题

时间:2018-02-15 12:53:17

标签: ruby-on-rails ruby ruby-on-rails-4 database-migration

我在迁移db

时遇到了问题
class CreateBlogoTaggings < ActiveRecord::Migration
  def change
    taggings_table = "#{Blogo.table_name_prefix}taggings"

    create_table(taggings_table) do |t|
      t.integer :post_id, null: false
      t.integer :tag_id , null: false
    end

    add_index taggings_table, :tag_id, unique: true
    add_index taggings_table, :post_id, unique: true

    if defined?(Foreigner)
      tags_table  = "#{Blogo.table_name_prefix}tags"
      posts_table = "#{Blogo.table_name_prefix}posts"

      add_foreign_key taggings_table, tags_table , column: :tag_id
      add_foreign_key taggings_table, posts_table, column: :post_id
    end
  end
end

迁移给了我

== 20180215114117 CreateBlogoTaggings: migrating ==============================
-- create_table("blogo_taggings")
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

PG::UndefinedTable: ERROR:  relation "posts" does not exist
: CREATE TABLE "blogo_taggings" ("id" serial primary key, "post_id" integer NOT NULL, CONSTRAINT fk_blogo_taggings_post_id FOREIGN KEY ("tpost_id") REFERENCES "posts" ("id"))

我甚至在change方法下方的create_table内对所有内容进行了评论,但仍然会出现同样的错误。

你能告诉我为什么会这样吗?

1 个答案:

答案 0 :(得分:1)

期望posts表可能由Post模型支持。尝试编辑CreateBlogoTaggings迁移文件。将所有post_id替换为blogo_post_id并再次运行迁移。