我遇到了同样的问题,除了@Raido 的解决方案解决了 db:migrate 的问题,但在创建租户时(例如,在 db:seed 期间),我仍然遇到 Apartment gem 的问题。
我发现 Rails 会自动添加enable_extension "postgis"
到 schema.rb,Apartment 使用它来创建租户模式。我不确定为什么 Apartment 不使用现有的 postgis 扩展(可能是创建租户时 search_path 的问题),但这会导致相同的错误。
解决方案(如果你可以这么称呼它)是简单地enable_extension "postgis"
从 schema.rb 中删除该行。这种方法的唯一问题是任何触发 schema.rb 刷新的后续迁移都会导致重新添加该行。
此外,我使用 Apartment 方法将 postgis 扩展添加到 shared_extensions 模式而不是它自己的模式中。我的 lib/tasks/db_extensions.rake 看起来像:
namespace :db do
desc 'Also create shared_extensions Schema'
task :extensions => :environment do
# Create Schema
ActiveRecord::Base.connection.execute 'CREATE SCHEMA IF NOT EXISTS shared_extensions;'
# Enable Hstore
ActiveRecord::Base.connection.execute 'CREATE EXTENSION IF NOT EXISTS HSTORE SCHEMA shared_extensions;'
# Enable uuid-ossp for uuid_generate_v1mc()
ActiveRecord::Base.connection.execute 'CREATE EXTENSION IF NOT EXISTS "uuid-ossp" SCHEMA shared_extensions;'
# Enable postgis extension for geographic data types
ActiveRecord::Base.connection.execute 'DROP EXTENSION IF EXISTS postgis;'
ActiveRecord::Base.connection.execute 'CREATE EXTENSION postgis WITH SCHEMA shared_extensions;'
ActiveRecord::Base.connection.execute 'GRANT USAGE ON SCHEMA shared_extensions to PUBLIC;'
puts 'Created extensions'
end
end
Rake::Task["db:create"].enhance do
Rake::Task["db:extensions"].invoke
end
Rake::Task["db:test:purge"].enhance do
Rake::Task["db:extensions"].invoke
end
我的 database.yml 看起来像:
postgis_options: &postgis_options
adapter: postgis
postgis_extension: postgis # default is postgis
postgis_schema: shared_extensions # default is public
default: &default
schema_search_path: 'public,shared_extensions'
encoding: utf8
<<: *postgis_options
...
production:
<<: *default
url: <%= ENV['DATABASE_URL'].try(:sub, /^postgres/, 'postgis') %>
不理想,但它正在工作。也许这会为 PostGIS 和 Apartment 节省一两个小时。我很想知道是否有人有比enable_extension
从 schema.rb 中删除调用更好的解决方案 :)