如何在PostgreSQL中修复丢失的PostGIS SRID?

bar*_*own 9 postgresql rspec postgis

运行我的Rspec测试套件时收到以下错误:

PG::InternalError: ERROR:  GetProj4StringSPI: Cannot find SRID (4326) in spatial_ref_sys
Run Code Online (Sandbox Code Playgroud)

我知道我启用了PostGIS扩展.我该如何解决?

bar*_*own 30

问题是有些东西从spatial_ref_sys表中删除了行.

就我而言,问题出在我的DatabaseCleaner配置中spec_helper.rb.它被配置为删除/截断表.

要防止该行为,请更改配置.对我来说,那是:

config.before(:suite) do
  DatabaseCleaner.strategy = :deletion, {:except => %w[spatial_ref_sys]}
  DatabaseCleaner.clean_with :truncation, {:except => %w[spatial_ref_sys]}
end
Run Code Online (Sandbox Code Playgroud)

现在,您需要重新生成该表中的行.使用命名的脚本spatial_ref_sys.sql来执行此操作.

我使用Postgres.app,所以运行该脚本的命令是:

/Applications/Postgres.app/Contents/MacOS/bin/psql -d database_name -f /Applications/Postgres.app/Contents/MacOS/share/contrib/postgis-2.0/spatial_ref_sys.sql
Run Code Online (Sandbox Code Playgroud)

您的命令可能略有不同.

  • 我注意到我所要做的就是重新运行`rake db:test:prepare`或rake spec来修复spacial_ref_sys表. (4认同)

tee*_*tee 9

运行rake db:test:prepare应该恢复中的值spatial_ref_sys.

更新:这在版本1.4.1中得到修复:https://github.com/DatabaseCleaner/database_cleaner/pull/328

database_cleaner版本1.4.0中存在错误,因此您需要指定表异常的架构:

DatabaseCleaner.strategy = :truncation, { except: ["public.spatial_ref_sys"] }
DatabaseCleaner.clean_with :truncation, { except: ["public.spatial_ref_sys"] }
Run Code Online (Sandbox Code Playgroud)

在1.4.0版中,架构作为表名的一部分返回.请参阅https://github.com/DatabaseCleaner/database_cleaner/pull/282