谁能解释我这个错误?

时间:2016-01-30 18:26:57

标签: ruby-on-rails ruby

我在使用Postgres时得到了这个。

9

这是我的车站和零售商模型。

零售商

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)

电台

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

这是我的电台迁移

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

我没有零售商迁移,因为它继承自用户。这是我的用户迁移

用户

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

将类型添加到用户

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

为用户添加额外属性

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

并向用户添加帐号

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

当我使用Sqlite3时,它没有错误,但我在使用postgres生产heroku时出错。所以我决定在开发中使用postgres,我看到了上面的错误,我无法理解。提前谢谢

2 个答案:

答案 0 :(得分:1)

retailers

此错误仅表示当您尝试在其他位置引用此表时,数据库中不存在{{1}}表。您只需确保在尝试在某些迁移中使用/引用它之前创建{{1}}表。

答案 1 :(得分:0)

错误来自迁移中的这一行:

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

外键选项意味着rails正在尝试在站表上的retailer_id和不存在的零售商表上的id之间创建外键。

虽然迁移经常与模型同时创建,但它们并没有真正联系 - 迁移并不知道零售商是STI模型。

据我所知,您需要从调用references中删除外键选项,并使用add_foreign_key单独添加外语:

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

您可能在开发过程中没有遇到过这种情况,因为早期版本的sqlite3不支持外键,当前版本要求激活它(请参阅docs):除非激活,否则它会忽略外来关键约束(只要它们在语法上是正确的)