Postgis 地理列在保存时抛出异常
Postgis geography column throwing exception on save
要事第一:
Ruby: 2.2.3
Rails: 4.2.5
我的数据库中有一个模型(我们称之为目标),它有一个 coordinates
列,它是通过这样的迁移创建的:
add_column :destinations, :coordinates, :st_point, geographic: true
我有一个回调设置来设置传入的 lat
和 lon
字段的坐标:
attr_accessor :lat, :lon
before_validation :set_coordinates
private
def set_coordinates
if lat.present? && lon.present?
self.coordinates = "POINT(#{lon} #{lat})"
end
end
这似乎工作正常。鉴于我已经传入了 lon
和 lat
它 returns 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 (, , , , , , ) 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
已解决
经过大量挖掘,在 activerecord-postgis-adapter gem 存储库上发现了 this issue。
似乎安装 meta_request gem 会导致此行为,即使只是在 :development
组中也是如此。
如果其他人遇到这个奇怪而晦涩的问题,我将保留它。
结束这一年的方式……:D
要事第一:
Ruby: 2.2.3
Rails: 4.2.5
我的数据库中有一个模型(我们称之为目标),它有一个 coordinates
列,它是通过这样的迁移创建的:
add_column :destinations, :coordinates, :st_point, geographic: true
我有一个回调设置来设置传入的 lat
和 lon
字段的坐标:
attr_accessor :lat, :lon
before_validation :set_coordinates
private
def set_coordinates
if lat.present? && lon.present?
self.coordinates = "POINT(#{lon} #{lat})"
end
end
这似乎工作正常。鉴于我已经传入了 lon
和 lat
它 returns 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 (, , , , , , ) 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
已解决
经过大量挖掘,在 activerecord-postgis-adapter gem 存储库上发现了 this issue。
似乎安装 meta_request gem 会导致此行为,即使只是在 :development
组中也是如此。
如果其他人遇到这个奇怪而晦涩的问题,我将保留它。
结束这一年的方式……:D