【问题标题】:ActionView::Template::Error (PG::UndefinedColumn: ERROR: column “user_id” does not existActionView::Template::Error (PG::UndefinedColumn: ERROR: 列“user_id”不存在
【发布时间】:2015-05-11 15:37:54
【问题描述】:

我在 heroku 上部署了我的项目。

crackeryourwardrobe.heroku.com 是我的网站。

当我点击 facebook 登录时,我在 heroku 日志中收到此错误

PG::UndefinedColumn: ERROR:  column "user_id" does not exist
SELECT "products".* FROM "products"  WHERE (user_id = 1)  

我的观点.rb

<% if session[:user_id] %>
  <% @account_products.order(created_at: :desc).in_groups_of(3) do |product_group| %>
    <div class = "row">
      <% product_group.compact.each do |product| %>
        <div class="col-md-4">

products.rb 类产品

wardrobe_item.rb 类 WardrobeItem

用户.rb

class User < ActiveRecord::Base
    has_many :wardrobe_items, dependent: :destroy

数据库.yml

default: &default
  adapter: sqlite3
  pool: 5
  timeout: 5000

development:
  <<: *default
  database: db/development.sqlite3

test:
  <<: *default
  database: db/test.sqlite3

production:
  <<: *default
  database: db/production.sqlite3

schema.rb

  create_table "products", force: true do |t|
    t.string   "title"
    t.text     "brand"
    t.string   "image_url"
    t.decimal  "price"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "item"
    t.integer  "user_id",    limit: 255

产品.rb

class Product < ActiveRecord::Base
    has_many :line_items, dependent: :destroy
    has_many :wardrobe_item, dependent: :destroy
    mount_uploader :item, ItemUploader 

end

wardrobe_item.rb

class WardrobeItem < ActiveRecord::Base
    belongs_to :product
    belongs_to :user
end

schema.rb

ActiveRecord::Schema.define(version: 20150310085128) do

  create_table "line_items", force: true do |t|
    t.integer  "product_id"
    t.integer  "cart_id"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.integer  "quantity",   default: 1
  end

  create_table "products", force: true do |t|
    t.string   "title"
    t.text     "brand"
    t.string   "image_url"
    t.decimal  "price"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "item"
    t.integer  "user_id",    limit: 255
  end

  create_table "stores", force: true do |t|
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "users", force: true do |t|
    t.string   "provider"
    t.string   "uid"
    t.string   "name"
    t.string   "oauth_token"
    t.datetime "oauth_expires_at"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.integer  "cart_id"
    t.integer  "wardrobe_item_id"
  end

  create_table "wardrobe_items", force: true do |t|
    t.integer  "product_id"
    t.integer  "user_id"
    t.datetime "created_at"
    t.datetime "updated_at"

我不知道如何解决它......请帮助......请......

【问题讨论】:

  • 你跑heroku rake db:migrate了吗?
  • 可能是错过了特定的迁移。尝试运行heroku rake db:migrate:up VERSION=your_migration_file_version
  • ActiveRecord::UnknownMigrationVersionError: No migration with version number 20150310075303
  • 另外,我添加了 migration ,并提交并部署了收到的 heroku
  • 未跟踪的文件:db/migrate/20150310075303_change_user_id_type_in_products.rb db/migrate/20150310085128_add_wardrobe_item_id_to_users.rb public/uploads/product/item/21/

标签: ruby-on-rails postgresql heroku


【解决方案1】:

这是因为用户模型和产品模型没有完全连接。

在用户模型中,添加这段代码

用户.rb

class User < ActiveRecord::Base
    has_many :wardrobe_items, dependent: :destroy
    has_many :products, through: :wardrobe_items # this is code

它将通过用户连接到产品:)

【讨论】:

  • 我仍然收到错误 "PG::UndefinedColumn: ERROR: column "user_id" of relationship "products" does not exist : ALTER TABLE "products" ALTER COLUMN "user_id" TYPE integer"
  • .. 我添加了我的另一个 model.rb
  • 你能给我完整的 schema.rb 和 user.rb 和 product.rb 和衣柜_items.rb 吗?
  • 我添加了我的 schema.rb 。并感谢您的回复
  • 1.从产品 2 中删除 user_id。检查您在用户、产品、衣柜中的关联性
猜你喜欢
  • 2017-03-14
  • 2019-09-21
  • 2013-10-10
  • 1970-01-01
  • 1970-01-01
  • 2015-10-18
  • 1970-01-01
  • 1970-01-01
  • 2018-03-26
相关资源
最近更新 更多