【问题标题】:undefined method `geometry' for ActiveRecord::ConnectionAdapters::PostgreSQLActiveRecord::ConnectionAdapters::PostgreSQL 的未定义方法“几何”
【发布时间】:2016-09-22 03:45:14
【问题描述】:

我使用 postgres 数据库创建了一个 rails 应用程序。我正在使用 postgis 扩展进行地理查询。该应用程序在我的开发(本地)机器上成功运行,但是在我运行 heroku run rake db:migrate 时在 heroku 服务器上部署我的代码后,它抛出了一个错误,说 undefined method geometry for ActiveRecord ConnectionAdapters PostgreSQL。 我在一些迁移中使用几何数据类型来存储纬度和经度。

请注意,我还在 heroku 上创建了 PostGIS extension。以及成功执行的不包含几何数据类型的迁移。 我的文件是:

宝石文件

ruby "2.3.0"
gem 'rails', '>= 5.0.0.beta3', '< 5.1'
gem 'pg', '~> 0.18'
gem 'rgeo'
gem 'rgeo-activerecord', "~> 5.0.0.beta"
gem "activerecord-postgis-adapter", "~> 4.0.0.beta2"

psql --version 为:9.5.2 在 heroku 服务器上

psql --version 是:9.4.7 在本地服务器上

database.yml

default: &default
  adapter: postgis
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
  <<: *default
  database: ad_development

production:
  <<: *default
  database: ad_production
  username: ad
  password: <%= ENV['DATABASE_PASSWORD'] %>

create_cities 迁移

def change
    create_table :cities do |t|
      t.string :name
      t.references :state, foreign_key: true
      t.geometry :lat_lan
end

heroku run rake db:migrate 仅在此处停止。

我很困惑我是否使用了不合适的 gem 或者我配置了错误的东西。请帮忙!

【问题讨论】:

    标签: ruby-on-rails postgresql heroku postgis


    【解决方案1】:

    url: &lt;%= ENV.fetch('DATABASE_URL', '').sub(/^postgres/, 'postgis') %&gt;

    编辑: 它将 url 方案 postgres://somewhere.com 替换为 postgis://somewhere.com。 它更改为 GIS“协议”,例如将 http 更改为 https

    【讨论】:

    • 感谢您提供此代码 sn-p,它可能会提供一些即时帮助。一个正确的解释would greatly improve 其教育价值通过展示为什么这是一个很好的解决问题的方法,并将使它对未来有类似但不相同的问题的读者更有用。请edit您的答案添加解释,并说明适用的限制和假设。
    【解决方案2】:

    如果使用 DATABASE_URL 环境变量设置数据库连接字符串,请确保它具有 postgis://(不是 postgres://)前缀。

    即。 postgis://username:password@db_server_url:5432/dbname

    【讨论】:

      【解决方案3】:

      对于适配器选项,您必须在 database.yml 文件中将 postgresql 替换为 postgis

      例如

      default: &default
        adapter: postgis
        encoding: unicode
        pool: <%= ENV["DB_POOL"] || ENV['MAX_THREADS'] || 5 %>
        url: <%= ENV['DATABASE_URL'] %>
      
      development:
        <<: *default
        database: yourapp_development
      

      【讨论】:

      • 你还有问题吗?
      • 但错误信息显示您使用的是 postgres 适配器 undefined method geometry for ActiveRecord ConnectionAdapters PostgreSQL 而不是 Postgis 适配器。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-07
      • 2020-10-13
      • 2018-09-01
      • 1970-01-01
      相关资源
      最近更新 更多