pdeffendol / spatial_adapter

Spatial Adapter for ActiveRecord and Rails 2.x and 3.0.x - no longer in active development (try RGeo for Rails 3.1+)
MIT License
198 stars 99 forks source link

Geom field not converted to hex_ewkb when using Model.create in Rails 3.1.0 #26

Open ryanwilliams opened 13 years ago

ryanwilliams commented 13 years ago

Creating a new record using

Foo.create(:geom => Point.from_x_y_z(-1.6, 2.8, -3.4, 4326))

Doesn't work, but this does:

f = Foo.create
f.geom = Point.from_x_y_z(-1.6, 2.8, -3.4, 4326)
f.save

With some debugging it appears that the PostgreSQLAdapter#quote method inside lib/spatial_adapter/postgresql.rb wasn't hit when using 'create', but was when updating. This was true even for non-geom fields.

Here is the output using the example provided in the README:

>> pt = TablePoint.new(
?>   :data => "Hello!", 
?>   :geom => Point.from_x_y_z(-1.6, 2.8, -3.4, 123))
   (0.5ms)  SELECT * FROM geometry_columns WHERE f_table_name = 'table_points'
=> #<TablePoint id: nil, data: "Hello!", geom: #<GeoRuby::SimpleFeatures::Point:0x104d5c988 @m=0.0, @x=-1.6, @with_m=false, @z=-3.4, @with_z=true, @srid=123, @y=2.8>>
>> pt.save
   (0.1ms)  BEGIN
  SQL (1.2ms)  INSERT INTO "table_points" ("data", "geom") VALUES ($1, $2) RETURNING "id"  [["data", "Hello!"], ["geom", #<GeoRuby::SimpleFeatures::Point:0x104d5c988 @m=0.0, @x=-1.6, @with_m=false, @z=-3.4, @with_z=true, @srid=123, @y=2.8>]]
   (0.2ms)  ROLLBACK
ActiveRecord::StatementInvalid: PGError: ERROR:  parse error - invalid geometry
HINT:  You must specify a valid OGC WKT geometry type such as POINT, LINESTRING or POLYGON
: INSERT INTO "table_points" ("data", "geom") VALUES ($1, $2) RETURNING "id"
    from /Library/Ruby/Gems/1.8/gems/activerecord-3.1.0.rc4/lib/active_record/connection_adapters/postgresql_adapter.rb:976:in `get_last_result'
    from /Library/Ruby/Gems/1.8/gems/activerecord-3.1.0.rc4/lib/active_record/connection_adapters/postgresql_adapter.rb:976:in `exec_cache'
    from /Library/Ruby/Gems/1.8/gems/activerecord-3.1.0.rc4/lib/active_record/connection_adapters/postgresql_adapter.rb:548:in `exec_query'
    from /Library/Ruby/Gems/1.8/gems/activerecord-3.1.0.rc4/lib/active_record/connection_adapters/abstract_adapter.rb:222:in `log'
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.1.0.rc4/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
    from /Library/Ruby/Gems/1.8/gems/activerecord-3.1.0.rc4/lib/active_record/connection_adapters/abstract_adapter.rb:217:in `log'
    from /Library/Ruby/Gems/1.8/gems/activerecord-3.1.0.rc4/lib/active_record/connection_adapters/postgresql_adapter.rb:546:in `exec_query'
    from /Library/Ruby/Gems/1.8/gems/activerecord-3.1.0.rc4/lib/active_record/connection_adapters/abstract/database_statements.rb:54:in `exec_insert'
    from /Library/Ruby/Gems/1.8/gems/activerecord-3.1.0.rc4/lib/active_record/connection_adapters/abstract/database_statements.rb:81:in `insert'
    from /Library/Ruby/Gems/1.8/gems/activerecord-3.1.0.rc4/lib/active_record/connection_adapters/abstract/query_cache.rb:14:in `insert'
    from /Library/Ruby/Gems/1.8/gems/activerecord-3.1.0.rc4/lib/active_record/relation.rb:68:in `insert'
    from /Library/Ruby/Gems/1.8/gems/activerecord-3.1.0.rc4/lib/active_record/persistence.rb:306:in `create'
    from /Library/Ruby/Gems/1.8/gems/activerecord-3.1.0.rc4/lib/active_record/timestamp.rb:51:in `create'
    from /Library/Ruby/Gems/1.8/gems/activerecord-3.1.0.rc4/lib/active_record/callbacks.rb:268:in `create'
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.1.0.rc4/lib/active_support/callbacks.rb:390:in `_run_create_callbacks'
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.1.0.rc4/lib/active_support/callbacks.rb:81:in `send'
... 4 levels...
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.1.0.rc4/lib/active_support/callbacks.rb:390:in `_run_save_callbacks'
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.1.0.rc4/lib/active_support/callbacks.rb:81:in `send'
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.1.0.rc4/lib/active_support/callbacks.rb:81:in `run_callbacks'
    from /Library/Ruby/Gems/1.8/gems/activerecord-3.1.0.rc4/lib/active_record/callbacks.rb:264:in `create_or_update'
    from /Library/Ruby/Gems/1.8/gems/activerecord-3.1.0.rc4/lib/active_record/persistence.rb:37:in `save'
    from /Library/Ruby/Gems/1.8/gems/activerecord-3.1.0.rc4/lib/active_record/validations.rb:50:in `save'
    from /Library/Ruby/Gems/1.8/gems/activerecord-3.1.0.rc4/lib/active_record/attribute_methods/dirty.rb:22:in `save'
    from /Library/Ruby/Gems/1.8/gems/activerecord-3.1.0.rc4/lib/active_record/transactions.rb:241:in `save'
    from /Library/Ruby/Gems/1.8/gems/activerecord-3.1.0.rc4/lib/active_record/transactions.rb:295:in `with_transaction_returning_status'
    from /Library/Ruby/Gems/1.8/gems/activerecord-3.1.0.rc4/lib/active_record/connection_adapters/abstract/database_statements.rb:183:in `transaction'
    from /Library/Ruby/Gems/1.8/gems/activerecord-3.1.0.rc4/lib/active_record/transactions.rb:208:in `transaction'
    from /Library/Ruby/Gems/1.8/gems/activerecord-3.1.0.rc4/lib/active_record/transactions.rb:293:in `with_transaction_returning_status'
    from /Library/Ruby/Gems/1.8/gems/activerecord-3.1.0.rc4/lib/active_record/transactions.rb:241:in `save'
    from /Library/Ruby/Gems/1.8/gems/activerecord-3.1.0.rc4/lib/active_record/transactions.rb:252:in `rollback_active_record_state!'
    from /Library/Ruby/Gems/1.8/gems/activerecord-3.1.0.rc4/lib/active_record/transactions.rb:240:in `save'
    from (irb):25>> 
ryanwilliams commented 13 years ago

Still broken in rc5. I can't work out why it's broken either, or I'd attempt to fix it myself.

Empact commented 13 years ago

Take a look at my fork - I have a working fix, but still an outstanding issue or two. Most notably, the schema dumper doesn't seem to be picking up its extensions, when run from within my rails project. The schema dumper specs pass.

Also note I've only been testing on postgres.

And do let me know how it works for you, I'll be curious to hear.

ileitch commented 13 years ago

@Empact Thank You for fixing this issue. I've been pulling my hair out all day!

mneumegen commented 12 years ago

@Empact Life saver! Thank you so much. This fix should be merged.

blackgold9 commented 12 years ago

Your fork totally fixed it for me. Thanks!

lukesaunders commented 12 years ago

Also fixed for me, thanks. Maybe turn it into a pull request?