【问题标题】:Postgis geography column throwing exception on savePostgis地理列在保存时抛出异常
【发布时间】:2016-03-23 10:13:12
【问题描述】:

第一件事:

红宝石: 2.2.3

导轨: 4.2.5

我的数据库中有一个模型(我们称之为目标),它有一个 coordinates 列,它是通过如下迁移创建的:

add_column :destinations, :coordinates, :st_point, geographic: true

我有一个回调设置来设置传入的 latlon 字段的坐标:

attr_accessor :lat, :lon

before_validation :set_coordinates

private

def set_coordinates
  if lat.present? && lon.present?
    self.coordinates = "POINT(#{lon} #{lat})"
  end
end

这似乎工作正常。鉴于我已经传入了 lonlat,它会返回 coordinates,如下所示:

coordinates: #<RGeo::Geographic::SphericalPointImpl:0x3ff3b1fa767c "POINT (144.96975 -37.810177)">

但是,当我尝试在模型上调用 save(带有或不带有 !)时,我遇到了以下错误:

destination.save
fatal: exception reentered
from /Users/myself/.rbenv/versions/2.2.3/gemsets/helpme/gems/activerecord-4.2.5/lib/active_record/transactions.rb:304:in `rescue in rollback_active_record_state!'

如果我不尝试设置此列,则模型会正确保存,并且是快乐的时光。不幸的是,这是模型功能中相当重要的一部分……

到目前为止我尝试过的事情:

这样设置坐标:

self.coordinates = RGeo::Geographic.spherical_factory(:srid => 4326).point(lon,lat)`

设置默认工厂:

RGeo::ActiveRecord::SpatialFactoryStore.instance.tap do |config|
  config.default = RGeo::Geographic.spherical_factory(srid: 4326)
end

我已运行所有检查以确保 Postgis 在我的数据库中工作,并且在 gemfile 中具有最新版本的 activerecord-postgis-adapter 并已安装。

只是希望那里的其他人可能遇到同样的事情并知道是什么原因造成的。

更新:

玩了一会儿(删除所有验证并在控制台中设置所有内容)后,我设法得到以下错误:

[5] pry(main)> destination.save

NoMethodError:
undefined method `reject!' for nil:NilClass 
# /Users/myself/.rbenv/versions/2.2.3/gemsets/helpme/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/transaction.rb:187:in   `rescue in within_new_transaction'
# /Users/myself/.rbenv/versions/2.2.3/gemsets/helpme/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/transaction.rb:201:in `within_new_transaction'
# /Users/myself/.rbenv/versions/2.2.3/gemsets/helpme/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
# /Users/myself/.rbenv/versions/2.2.3/gemsets/helpme/gems/activerecord-4.2.5/lib/active_record/transactions.rb:220:in `transaction'
# /Users/myself/.rbenv/versions/2.2.3/gemsets/helpme/gems/activerecord-4.2.5/lib/active_record/transactions.rb:348:in `with_transaction_returning_status'
# /Users/myself/.rbenv/versions/2.2.3/gemsets/helpme/gems/activerecord-4.2.5/lib/active_record/transactions.rb:291:in `save!'
# /Users/myself/.rbenv/versions/2.2.3/gemsets/helpme/gems/activesupport-4.2.5/lib/active_support/notifications.rb:166:in `instrument'
# /Users/myself/.rbenv/versions/2.2.3/gemsets/helpme/gems/activesupport-4.2.5/lib/active_support/notifications.rb:166:in `instrument'
# ./spec/spec_helper.rb:136:in `is_expected_to_have_a_valid_factory'
# ./spec/models/issue_spec.rb:25:in `block (3 levels) in <top (required)>'
# /Users/myself/.rbenv/versions/2.2.3/gemsets/helpme/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:268:in `load'
# /Users/myself/.rbenv/versions/2.2.3/gemsets/helpme/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:268:in `block in load'
# /Users/myself/.rbenv/versions/2.2.3/gemsets/helpme/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:240:in `load_dependency'
# /Users/myself/.rbenv/versions/2.2.3/gemsets/helpme/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:268:in `load'
# -e:1:in `<main>'
# ------------------
# --- Caused by: ---
# fatal:
#   exception reentered
#

不确定这是否真的有帮助。但我猜信息越多越好

更新 2:

日志输出(现在看这里没什么帮助):

  SQL (4.7ms)  INSERT INTO "destinations" ("customer_id", "profession_id", "address", "credit_card_id", "coordinates", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id"  [["customer_id", "ca5213d1-bdd3-4090-8f5a-11524d9c4dfa"], ["profession_id", "f18d3c54-9315-4415-a832-34299cfa8c5b"], ["address", "45756 Thalia Spurs, O'Connor ACT 6168"], ["credit_card_id", "a6f38078-cce0-4804-8c38-6bca24948c16"], ["coordinates", "0020000001000010e640621f083126e979c042e7b3e1437c57"], ["created_at", "2015-12-17 03:20:51.715434"], ["updated_at", "2015-12-17 03:20:51.715434"]]
  (0.3ms)  ROLLBACK TO SAVEPOINT active_record_2

【问题讨论】:

  • 是否还有可用的堆栈跟踪,特别是指向您自己代码中的一行的内容?
  • @Toby1Kenobi 这就是打印到控制台的所有内容。用日志文件编辑。
  • @Toby1Kenobi 设法获得了更大的堆栈跟踪...is_expected_to_have_a_valid_factory 只是一个帮助方法,用于构建工厂并检查它是否为有效对象。

标签: ruby-on-rails ruby postgresql activerecord postgis


【解决方案1】:

已解决

经过大量挖掘后,在 activerecord-postgis-adapter gem repo 上遇到了this issue

似乎安装了meta_request gem 会导致这种行为,即使只是在:development 组中也是如此。

如果其他人遇到这个奇怪而晦涩的问题,我会保留它。

结束这一年的方式……:D

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多