【问题标题】:PostgreSQL point type in Rails migrationRails 迁移中的 PostgreSQL 点类型
【发布时间】:2014-03-06 15:47:33
【问题描述】:

我想使用 PostgreSQL 中的 point 类型。我已经完成了:

rails g model Test point:point

生成的迁移是:

class CreateTests < ActiveRecord::Migration
  def change
    create_table :tests do |t|
      t.point :point

      t.timestamps
    end
  end
end

当我跑步时:

rake db:migrate

结果是:

    ==  CreateTests: migrating ====================================================
    -- create_table(:tests)
    rake aborted!
    An error has occurred, this and all later migrations canceled:

    undefined method `point' for #<ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::TableDefinition:0x000000038991a0>/home/i/Dropbox/programming/ruby/rails_pg_for_tests/db/migrate/20140306151700_create_tests.rb:4:in `block in change'
    /home/i/.rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/schema_statements.rb:184:in `create_table'
    /home/i/.rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.2/lib/active_record/migration.rb:625:in `block in method_missing'
    /home/i/.rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.2/lib/active_record/migration.rb:597:in `block in say_with_time'
...

可能我需要安装 PostGIS,但如果我在 PostgreSQL 中有点类型,我不明白为什么。而且我只需要存储纬度和经度,没有任何其他选项。

如何使用这种点类型或在这种情况下使用什么更好?

谢谢。

【问题讨论】:

  • PostgreSQL 有点类型,不代表 ActiveRecord 支持。
  • point 类型来自 PostgreSQL,与 PostGIS 的geometry 类型无关。

标签: ruby-on-rails ruby postgresql ruby-on-rails-4


【解决方案1】:

您可以将数据类型指定为字符串

t.column 'point', 'point'

【讨论】:

  • 第一个point是列名,第二个是列类型。
【解决方案2】:

Rails 已经支持点数年了。测试用例自 rails 4.2 beta 起就已存在。

如果您使用的是 rails 4.2 或更高版本。你不应该得到那个错误。我还没有在 4.2 beta 上测试过它。我只在 Rails 5.2 上测试它,它不需要任何额外的 gem。

https://github.com/rails/rails/blob/24adc20ace69ec0fcf317e0a8d91d112478ba015/activerecord/lib/active_record/connection_adapters/postgresql/oid/point.rb

用法:

迁移

class CreateAddresses < ActiveRecord::Migration[5.2]
  def change
    create_table :addresses do |t|
      t.point   :coordinate
    end
  end
end

创建新记录

Address.create coordinate: ActiveRecord::Point.new(1, 1)

【讨论】:

  • 这是非常受欢迎的更新,尤其是在编写最前沿的 Rails 6 和 activerecord-postgis-adapter gem 尚不可用时。
【解决方案3】:

您收到undefined method 'point' for #&lt;ActiveRecord:... 迁移代码错误

t.point :point

因为point 方法在ActiveRecord 中不存在。

ActiveRecord原生支持关注data types,

 :primary_key, :string, :text, :integer, :float, :decimal, :datetime, :timestamp, 
 :time, :date, :binary, :boolean

查看 ActiveRecord 的 official documentation 以获取相同的信息。

正如@bridiver 所建议的那样,您可以选择使用t.column :point, 'point' 但在ActiveRecord 文档中引用的ActiveRecord(如上所列)的支持 数据类型指定以外的类型

这与数据库无关,通常应该避免。

更新

我会建议利用@euricovidal 所建议的activerecord-postgis-adapter gem 的好处。它是 pgsql 的地理空间扩展,它充当 PostGIS 查询函数的包装器,并允许您编写地理感知迁移。 它将允许您创建一个 point 类型,并且使用它您当前的语法应该可以工作。

例如参考PostGIS ActiveRecord Adapter: Creating Spatial Tables。您还需要将database.yml 文件中的adapter 更新为postgis 才能正常工作。

【讨论】:

  • 请添加评论以描述您为什么拒绝投票,以便我可以改进答案,而不仅仅是拒绝投票。
  • 我投了反对票,因为它是错误的。您可以使用我在下面概述的列来指定任意类型。
  • @bridiver 谢谢你。我在回答中添加了解释。
  • 使用 postgis 会有同样的警告,它不会与数据库无关
  • @bridiver 当它是一个特定于数据库的东西时,在这两种情况下,它当然与数据库无关。对于 OP(考虑到可维护性和可用性问题)来说,他是否希望选择主要不被使用中的 gem 支持的东西,或者他使用专门为需求设计的 gem 的扩展,仍然是一个偏好问题。
【解决方案4】:

如果您使用的是 activerecord-postgis-adapter,则需要将 database.yml 上的适配器更改为 postgis。

https://github.com/dazuma/activerecord-postgis-adapter#installation-and-configuration

【讨论】:

    【解决方案5】:

    您很可能需要编辑您的 config\database.yml 以引用 postgis 作为您的数据库适配器

    default: &default
      adapter: postgis
      encoding: unicode
      pool: 5
      timeout: 5000
      host: localhost
    
    development:
      <<: *default
      database: learning_rails
      username: brentpayne
      password:
    

    您可能还想编辑迁移以将:point 指定为geographic,以便计算距离。

      t.point :point, :geographic => true
    

    但是当我将 adapter 设置为 postgresql 时,您遇到的错误与我的相同。将其更改为 postgis 解决了我的问题。

    参考:https://github.com/rgeo/activerecord-postgis-adapter#installation-and-configuration

    【讨论】:

      【解决方案6】:

      如果您安装georuby,点数据类型将变为可用。

      宝石文件

      gem 'pg'
      gem 'georuby'
      

      在迁移中

      def change
        create_table :space_table do |t|
          t.point :location
          t.timestamps
        end
      end
      

      您可以使用model_object.location = [-33.233, 123.123]保存积分

      【讨论】:

        猜你喜欢
        • 2014-03-24
        • 2015-08-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-18
        • 2013-08-28
        • 2010-12-30
        相关资源
        最近更新 更多