我在使用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)
這裏是我的車站和零售商模型。
零售商
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
並添加帳號用戶
class AddAccountNumberToUsers < ActiveRecord::Migration
def change
add_column :users, :account_number, :string
add_index :users, :account_number
end
end
它使用沒有錯誤,當我使用Sqlite3但我在生產heroku使用postgres中的錯誤。所以我決定在開發中使用postgres,我看到了上面的錯誤,我無法理解。謝謝你在前進
零售商從用戶繼承。所以我想零售商表是不需要的。它只是用戶表。所以我應該將代碼更改爲has_many用戶等? – suyesh
你仍然可以使用'零售商',但指定類名稱爲:'用戶'就像這樣:'belongs_to:retailer,class_name:'User'' and'has_many:retailers,class_name:'User'' –
Ahhh讓我試試。 – suyesh