谁能解释我这个错误吗? - can anyone explain me this error?

- 此内容更新于:2016-02-02
主题:

我这个当我使用Postgres。这是我的电台和零售商模型。零售商站在这里是我的站迁移有零售商从用户迁移,因为它的继承。这是我的用户迁移用户类型用户添加额外的属性添加到用户和帐号添加到用户使用没有错误当我使用Sqlite3但是我得到错误在生产中使用postgresheroku。所以我决定用postgresdevelopemnt和我看见上面的错误,我无法理解。提前谢谢你

原文:

I am getting this while i use Postgres.

rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

PG::UndefinedTable: ERROR:  relation "retailers" does not exist
: ALTER TABLE "stations" ADD CONSTRAINT "fk_rails_57ee36b830"
FOREIGN KEY ("retailer_id")
  REFERENCES "retailers" ("id")
/home/suyesh/Desktop/Petrohub_main/db/migrate/20160104152245_create_stations.rb:3:in `change'
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  relation "retailers" does not exist
: ALTER TABLE "stations" ADD CONSTRAINT "fk_rails_57ee36b830"
FOREIGN KEY ("retailer_id")
  REFERENCES "retailers" ("id")
/home/suyesh/Desktop/Petrohub_main/db/migrate/20160104152245_create_stations.rb:3:in `change'
PG::UndefinedTable: ERROR:  relation "retailers" does not exist
/home/suyesh/Desktop/Petrohub_main/db/migrate/20160104152245_create_stations.rb:3:in `change'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

Here are my stations and retailers model.

Retailer

class Retailer < User
  has_many :stations
  has_many :retailer_suppliers
  has_many :suppliers , through: :retailer_suppliers, as: :connections
end

Stations

class Station < ActiveRecord::Base
  belongs_to :retailer
  has_many :tanks
end

Here is my Station Migration

class CreateStations < ActiveRecord::Migration
  def change
    create_table :stations do |t|
      t.string :brand
      t.string :business_name
      t.string :tax_id
      t.string :phone_number
      t.string :contact_person
      t.string :cell_number
      t.string :address1
      t.string :address2
      t.string :city
      t.string :state
      t.string :zip
      t.string :station_reg_number
      t.references :retailer, index: true, foreign_key: true

      t.timestamps null: false
    end
  end
end

I dont have retailer migration because its inheriting from the User. Here is my User migration

User

class DeviseCreateUsers < ActiveRecord::Migration
  def change
    create_table(:users) do |t|
      ## Database authenticatable
      t.string :email,              null: false, default: ""
      t.string :encrypted_password, null: false, default: ""

      ## Recoverable
      t.string   :reset_password_token
      t.datetime :reset_password_sent_at

      ## Rememberable
      t.datetime :remember_created_at

      ## Trackable
      t.integer  :sign_in_count, default: 0, null: false
      t.datetime :current_sign_in_at
      t.datetime :last_sign_in_at
      t.string   :current_sign_in_ip
      t.string   :last_sign_in_ip

      ## Confirmable
      # t.string   :confirmation_token
      # t.datetime :confirmed_at
      # t.datetime :confirmation_sent_at
      # t.string   :unconfirmed_email # Only if using reconfirmable

      ## Lockable
      # t.integer  :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
      # t.string   :unlock_token # Only if unlock strategy is :email or :both
      # t.datetime :locked_at


      t.timestamps null: false
    end

    add_index :users, :email,                unique: true
    add_index :users, :reset_password_token, unique: true
    # add_index :users, :confirmation_token,   unique: true
    # add_index :users, :unlock_token,         unique: true
  end
end

Add Type to User

class AddTypeToUsers < ActiveRecord::Migration
  def change
    add_column :users, :type, :string
  end
end

Add extra attributes to user

class AddExtraToUsers < ActiveRecord::Migration
  def change
    add_column :users, :first_name, :string
    add_column :users, :last_name, :string
    add_column :users, :phone_number, :string
    add_column :users, :cell_number, :string
    add_column :users, :tax_id, :string
    add_column :users, :business_name, :string
    add_column :users, :address1, :string
    add_column :users, :address2, :string
    add_column :users, :city, :string
    add_column :users, :state, :string
    add_column :users, :zip_code, :string
    add_column :users, :years_in_business, :string
  end
end

And add account number to user

class AddAccountNumberToUsers < ActiveRecord::Migration
  def change
    add_column :users, :account_number, :string
    add_index :users, :account_number
  end
end

It works with no errors when i use Sqlite3 but i get errors in Production heroku using postgres. so i decided to use postgres in developemnt and i saw the above errors which i cannot understand. Thank you in advance

解决方案:
这个错误仅仅意味着数据库中的表不存在当你试图引用另一个地方。你所需要的是确保你创建零售商表在你尝试使用一些迁移/引用它。
原文:
PG::UndefinedTable: ERROR:  relation "retailers" does not exist

This error simply means that the retailers table is not present in your database when you try to reference this in another place. All you need is to make sure you create retailers table before you try to use/reference it in some some migration.

楼主:零售商继承用户。所以我认为零售商表不需要。它只是用户表。所以我应该改变的代码has_many用户等?

(原文:Retailers inherit from User. SO i figured retailer table is not needed. Its just User table. so should i change the code to has_many users and such?)

网友:你仍然可以使用但指定类名:像这样:和

(原文:You can still use retailer but specify the class name as: User like such: belongs_to :retailer, class_name: 'User' and has_many :retailers, class_name: 'User')

楼主:啊让我试试。

(原文:Ahhh let me try that.)

楼主:我如何做,在“通过”协会吗?

(原文:How to i do that in "through" association?)

楼主:仍然得到同样的错误。我应该改变迁移使用用户而不是零售商和供应商?我在协会做零售商与类用户吗?

(原文:still getting same error after that. should i change the migration to use User instead of retailers and suppliers? and in association i do retailers with class user?)

解决方案:
错误来自这条线在迁移:外键选项意味着rails正试图创建一个外键retailer_id之间站表和非existant零售商id表。尽管迁移经常同时创建一个模型,他们不是真正的连接——迁移不知道零售商是一个STI模型。只要我可以告诉您需要删除外键选项调用和单独添加外国:你可能没有遇到这个开发因为sqlite3的早期版本中不支持外键,当前版本需要被激活(见文档):除非激活只是忽略了外键约束(只要它们语法正确)
原文:

The error is coming from this line in your migration:

  t.references :retailer, index: true, foreign_key: true

The foreign key option means that rails is trying to create a foreign key between retailer_id on the stations table and id on the non existant retailers table.

Although migrations are frequently created at the same time as a model, they're not really connected - the migration doesn't know that retailer is an STI model.

As far as I can tell you will need to remove the foreign key option from the call to references and add the foreign separately with add_foreign_key:

add_foreign_key :stations, :users, column: "retailer_id"

You probably didn't encounter this in development because earlier versions of sqlite3 didn't support foreign keys and current versions require that it be activated (see docs): unless activated it just ignores foreign key constraints (as long as they are syntactically correct)