繁体   English   中英

当我将Rails应用程序部署到heroku时,外键消失了

[英]When I deploy my rails app to heroku a foreign key disappears

我下面显示的所有代码都是我从heroku克隆的代码。

我的应用程式中有这种关系:

架构

  create_table "spots", id: false, force: :cascade do |t|
    t.string  "id",        null: false
    t.integer "bid_cents", null: false
    t.integer "ask_cents", null: false
  end

  create_table "products", id: false, force: :cascade do |t|
    t.string   "id",                                       null: false
    t.string   "type",                                     null: false
    t.text     "description",                              null: false
    t.decimal  "weight",                                   null: false
    t.string   "metal",                                    null: false
    t.string   "spot_id",                                  null: false
    t.integer  "elemetal_capital_premium_cents"
    t.integer  "goldtrex_markup_cents",                    null: false
    t.datetime "created_at",                               null: false
    t.datetime "updated_at",                               null: false
    t.integer  "shopify_id",                     limit: 8, null: false
  end

add_index "products", ["spot_id"], name: "index_products_on_spot_id", using: :btree

从模型中提取

从产品中提取

class Product < ActiveRecord::Base
  self.primary_key = :id

  belongs_to :spot

  before_create :shopify_create
  before_update :shopify_update
  ...

从现场提取

class Spot < ActiveRecord::Base
  self.primary_key = :id

  monetize :bid_cents
  monetize :ask_cents

  has_many :products

从产品继承到ElemetalCapitalProduct和GoldtrexProduct的提取

class ElemetalCapitalProduct < Product

  def self.available_products
    ElemetalCapital::Product.all.size
  end

class GoldtrexProduct < Product
  validates_presence_of :id, 
                        :description, 
                        :weight, 
                        :metal, 
                        :spot_id,
                        :goldtrex_markup_cents

我正在尝试使用的方法

def self.update_products!
    ec_products = ElemetalCapital::Product.all
    transaction do
      ec_products.each do |ec_product|
        product = ElemetalCapitalProduct.where(id: ec_product.id).first_or_initialize
        product.spot_id = ec_product.spot
        product.goldtrex_markup = 25
        product.description = ec_product.description
        product.metal = ec_product.metal
        product.weight = ec_product.weight
        product.elemetal_capital_premium = ec_product.premiumBuy
        product.save!
      end
    end

在控制器中:

  def master_update
    ElemetalCapitalProduct.update_products!
    redirect_to elemetal_capital_products_path
  end

问题

如果我在可以运行的本地计算机上运行该方法。 但是,当我部署到heroku并尝试使用该方法时,出现此错误:

Started GET "/elemetal_capital_products/elemetal_capital_api_dashboard" for 101.176.220.53 at 2015-07-22 15:11:55 +0000
2015-07-22T15:11:55.383259+00:00 app[web.1]:   User Load (2.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 1]]
2015-07-22T15:11:55.380497+00:00 app[web.1]: Processing by ElemetalCapitalProductsController#elemetal_capital_api_dashboard as HTML
2015-07-22T15:11:55.386042+00:00 app[web.1]:   Rendered shared/_page_title.erb (0.1ms)
2015-07-22T15:11:55.900630+00:00 heroku[router]: at=info method=GET path="/elemetal_capital_products/elemetal_capital_api_dashboard" host=goldtrex.herokuapp.com request_id=a6b622f6-aa23-4a77-817f-deb3d236b7eb fwd="101.176.220.53" dyno=web.1 connect=0ms service=524ms status=200 bytes=5498
2015-07-22T15:11:55.886397+00:00 app[web.1]:   Rendered elemetal_capital_products/elemetal_capital_api_dashboard.html.erb within layouts/application (501.3ms)
2015-07-22T15:11:55.888365+00:00 app[web.1]: Completed 200 OK in 508ms (Views: 504.2ms | ActiveRecord: 2.0ms)
2015-07-22T15:11:57.753099+00:00 app[web.1]: Started POST "/elemetal_capital_products/master_update" for 101.176.220.53 at 2015-07-22 15:11:57 +0000
2015-07-22T15:11:57.926609+00:00 app[web.1]:    (1.9ms)  BEGIN
2015-07-22T15:11:57.944657+00:00 app[web.1]: 
2015-07-22T15:11:57.942913+00:00 app[web.1]:    (2.0ms)  ROLLBACK
2015-07-22T15:11:57.944661+00:00 app[web.1]: NoMethodError (undefined method `spot_id=' for #<ElemetalCapitalProduct:0x007f00909740b8>):
2015-07-22T15:11:57.944664+00:00 app[web.1]:   app/models/product.rb:95:in `each'
2015-07-22T15:11:57.944663+00:00 app[web.1]:   app/models/product.rb:97:in `block (2 levels) in update_products!'
2015-07-22T15:11:57.944665+00:00 app[web.1]:   app/models/product.rb:95:in `block in update_products!'
2015-07-22T15:11:57.944667+00:00 app[web.1]:   app/models/product.rb:94:in `update_products!'
2015-07-22T15:11:57.944668+00:00 app[web.1]:   app/controllers/elemetal_capital_products_controller.rb:34:in `master_update'
2015-07-22T15:11:57.944670+00:00 app[web.1]: 
2015-07-22T15:11:57.944671+00:00 app[web.1]: 
2015-07-22T15:11:57.756503+00:00 app[web.1]: Processing by ElemetalCapitalProductsController#master_update as HTML
2015-07-22T15:11:57.756599+00:00 app[web.1]:   Parameters: {"authenticity_token"=>"R8o+PLuoFdbHUttiEadQ1msEhOXt0TYZQz/bF6378zl2RdHVMePsYXH7fTPB5P///zSaKNWgrLOZSIN17D94eA=="}
2015-07-22T15:11:57.760060+00:00 app[web.1]:   User Load (2.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 1]]
2015-07-22T15:11:57.933116+00:00 app[web.1]:   ElemetalCapitalProduct Load (2.2ms)  SELECT  "products".* FROM "products" WHERE "products"."type" IN ('ElemetalCapitalProduct') AND "products"."id" = $1  ORDER BY "products"."id" ASC LIMIT 1  [["id", "SBAR10-OPM"]]
2015-07-22T15:11:57.943605+00:00 app[web.1]: Completed 500 Internal Server Error in 187ms
2015-07-22T15:11:57.951593+00:00 heroku[router]: at=info method=POST path="/elemetal_capital_products/master_update" host=goldtrex.herokuapp.com request_id=075d778d-acfb-4438-9f0d-a59b5f409a16 fwd="101.176.220.53" dyno=web.1 connect=1ms service=197ms status=500 bytes=1741
  • 我已经在生产应用程序上运行了迁移,所有迁移都已完成。

奇怪的东西

如果我必须在heroku中使用Rails控制台,并且键入Product以查看属性,则看不到spot_id

Product
=> Product(id: string, type: string, description: text, weight: decimal, metal: string, elemetal_capital_premium_cents: integer, goldtrex_markup_cents: integer, created_at: datetime, updated_at: datetime, shopify_id: integer)

但是,如果我转到本地计算机,则可以看到spot_id:

[3] pry(main)> Product
=> Product(id: string, type: string, description: text, weight: decimal, metal: string, spot_id: string, elemetal_capital_premium_cents: integer, goldtrex_markup_cents: integer, created_at: datetime, updated_at: datetime, shopify_id: integer)

有人可以帮我解决这个问题吗? ,我将heroku代码与我的master分支进行了比较,它们是相同的。 我正在考虑运行迁移以添加spot_id,但这没有任何意义,因为在架构中存在关系,但是在模型中显然不存在关系。

谢谢。

在部署代码时,Heroku无法运行迁移,您必须在推送代码后立即运行迁移:

 heroku run rake db:migrate

这就是为什么您无法在产品模型中看到新列spot_id的原因。

我使用以下命令克隆heroku应用程序:

% heroku git:clone -a the-app

将我的Heroku存储库与本地存储库进行比较后,我发现代码没有任何区别。 一切都一样,所以我决定重设数据库。

 % heroku pg:reset DATABASE

我运行了迁移,最后出现了spot_id,然后我可以更新产品。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM