开发者

Why does ActiveRecord fail to output valid SQL INSERT statements for GeoRuby fixtures?

开发者 https://www.devze.com 2023-03-03 12:13 出处:网络
I am creating a rails application with geographical locations using spatial_adapter and GeoRuby. When running the actual application in development mode, everything works fine, but when trying to use

I am creating a rails application with geographical locations using spatial_adapter and GeoRuby. When running the actual application in development mode, everything works fine, but when trying to use YAML fixtures, I get a MySQL syntax error. I am using the following fixture:

one:
  name: Event One
  starts_at: 2011-04-20 12:00:00
  ends_at: 2011-04-20 16:00:00
  topics:
    - topic1
    - topic2
  pos: !ruby/object:Point
    x: 10
    y: 10

Running this, either with Test::Ruby or RSpec gives the following error:

ActiveRecord::StatementInvalid: Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds
to your MySQL server version for the right syntax to use near '), '2011-05-05 20:57:34', '2011-05-05 20:57:34', 908014535)'
at line 1: INSERT INTO `events` (`name`, `starts_at`, `ends_at`, `pos`, `created_at`, `updated_at`, `id`) VALUES ('Lindy Jam', '2011-04-20 12:00:00', '2011-04-20 16:00:00', GeomFromWKB(0x010100000000000000000024400000000000002440,), '2011-05-05 20:57:34', '2011-05-05 20:57:34', 908014535)

The key seems to be at this point:

GeomFromWKB(0x010100000000000000000024400000000000002440,),

With an extra comma inside the parenthesis. I don't know where this is coming from, but it is working for regular insertions in the development-mode app.

The stack trace is:

/var/lib/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/connection_adapters/abstract_adapter.rb:207:in `rescue in log'
/var/lib/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/connection_adapters/abstract_adapter.rb:199:in `log'
/v开发者_Go百科ar/lib/gems/1.9.1/gems/mysql2-0.2.6/lib/active_record/connection_adapters/mysql2_adapter.rb:314:in `execute'
/var/lib/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/connection_adapters/abstract/database_statements.rb:239:in `insert_fixture'
/var/lib/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/fixtures.rb:646:in `block in insert_fixtures'
/var/lib/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/fixtures.rb:582:in `each'
/var/lib/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/fixtures.rb:582:in `insert_fixtures'
/var/lib/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/fixtures.rb:526:in `block (4 levels) in create_fixtures'
/var/lib/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/fixtures.rb:526:in `each'
/var/lib/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/fixtures.rb:526:in `block (3 levels) in create_fixtures'
/var/lib/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/connection_adapters/abstract/database_statements.rb:139:in `transaction'
/var/lib/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/fixtures.rb:524:in `block (2 levels) in create_fixtures'
/var/lib/gems/1.9.1/gems/mysql2-0.2.6/lib/active_record/connection_adapters/mysql2_adapter.rb:232:in `disable_referential_integrity'
/var/lib/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/fixtures.rb:515:in `block in create_fixtures'
/var/lib/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/benchmarkable.rb:55:in `silence'
/var/lib/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/fixtures.rb:514:in `create_fixtures'
/var/lib/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/fixtures.rb:973:in `load_fixtures'
/var/lib/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/fixtures.rb:938:in `setup_fixtures'
/var/lib/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/callbacks.rb:425:in `_run_setup_callbacks'
/var/lib/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/testing/setup_and_teardown.rb:34:in `run'

I am running rails 3.0.7 with Ruby 1.9.2 on Ubuntu 11.04.


Turns out it's an error in spatial_adapter. It expects there to be an srid attribute on the geometry objects, but for some reason one isn't set when the fixtures create a GeoRuby object.

A temporary workaround is to manually add an srid attribute in the YAML file like this:

one:
  name: Event One
  starts_at: 2011-04-20 12:00:00
  ends_at: 2011-04-20 16:00:00
  topics:
    - topic1
    - topic2
  pos: !ruby/object:Point
    srid: -1
    x: 10
    y: 10

Where -1 is just a placeholder for "unspecified srid".

0

精彩评论

暂无评论...
验证码 换一张
取 消