为什么 ActiveRecord 无法为 GeoRuby 设备输出有效的 SQL INSERT 语句?

发布于 2024-11-05 12:07:04 字数 3473 浏览 4 评论 0原文

我正在使用 spatial_adapter 和 GeoRuby 创建一个具有地理位置的 Rails 应用程序。在开发模式下运行实际应用程序时,一切正常,但当尝试使用 YAML 固定装置时,我收到 MySQL 语法错误。我正在使用以下固定装置:

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

使用 Test::Ruby 或 RSpec 运行它会给出以下错误:

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)

关键似乎在这一点上:

GeomFromWKB(0x010100000000000000000024400000000000002440,),

括号内有一个额外的逗号。我不知道这是从哪里来的,但它适用于开发模式应用程序中的常规插入。

堆栈跟踪是:

/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'
/var/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'

我在 Ubuntu 11.04 上运行 Rails 3.0.7 和 Ruby 1.9.2。

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'
/var/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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

属性 2024-11-12 12:07:04

结果发现这是 spatial_adapter 中的错误。它期望几何对象上有一个 srid 属性,但由于某种原因,当装置创建 GeoRuby 对象时没有设置该属性。

临时解决方法是在 YAML 文件中手动添加 srid 属性,如下所示:

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

其中 -1 只是“未指定 srid”的占位符。

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".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文