【发布时间】:2021-12-19 17:06:23
【问题描述】:
这似乎是一个常见错误,我已经检查了我能找到的所有解决方案(只有大约 4 个,几乎所有解决方案都涉及错误配置)。我没有使用heroku,但我使用的是docker。我正在使用泊坞窗图像python:3.9.7 和postgis/postgis:10-3.1-alpine。
我的 Dockerfile 包含以下行:
ARG BUILD_ENV=production
# ...
FROM python:3.9.7 as apiserver
#...
RUN apt-get update && \
apt-get install --no-install-recommends -y apt-utils build-essential sudo git wget curl ca-certificates nginx openssl libpq-dev expect libmagic-dev graphviz python3-gdal python3-pandas postgis binutils libproj-dev gdal-bin python3-djangorestframework-gis
# ...
RUN pip install --upgrade pip && pip install -r requirements/${BUILD_ENV}.txt
# ...
这应该确保我需要为 django 构建 postgis 库的所有依赖项都存在,所以当我运行 pip install 时,它将拥有所需的一切。它比它需要的更详细,因为 GeoDjango 文档说要安装 postgis binutils libproj-dev gdal-bin,而 drf-gis 将所有 GeoDjango 库作为依赖项。我打算使用 drf-gis 包,但目前没有使用它。
requirements/production.txt
django-cors-headers==3.5.0
django-extensions==3.0.9
django-filter==2.4.0
# ...
Django==3.1.13
# ...
djangorestframework==3.12.4
djangorestframework-gis==0.17
# ...
我在 INSTALLED_APPS 中启用了正确的应用程序:
INSTALLED_APPS = [
'django.contrib.contenttypes',
'django.contrib.admin',
'django.contrib.admindocs',
'django.contrib.auth',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.gis',
'django_extensions',
'rest_framework',
'rest_framework.authtoken',
# 'rest_framework_gis',
'django.contrib.sites',
'django_filters',
'apiserver', # api v1 app doesn't use geodjango
'apiserver_v2', # api v2 uses geodjango
# ...
]
我还将以下内容设置为我的数据库配置:
DATABASES = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'HOST': os.environ.get('PGHOST'),
'PORT': 5432,
'NAME': os.environ.get('PGDATABASE'),
'USER': os.environ.get('PGUSER'),
'PASSWORD': os.environ.get('PGPASSWORD'),
'CONN_MAX_AGE': os.environ.get('DB_CONN_MAX_AGE', 500),
},
}
我还有以下迁移作为我的应用程序中的第一个迁移。我在使用 SpatiaLite 的测试中不需要它,但我在此处添加了它以完成文档。
0000_postgis.py
from django.contrib.postgres.operations import CreateExtension
from django.db import migrations
class Migration(migrations.Migration):
operations = [
CreateExtension('postgis'),
CreateExtension('postgis_raster'),
]
此迁移按预期进行,并且扩展程序似乎已安装:
apiserver_db_name=# \dx
List of installed extensions
Name | Version | Schema | Description
----------------+---------+------------+------------------------------------------------------------
plpgsql | 1.0 | pg_catalog | PL/pgSQL procedural language
postgis | 3.1.4 | public | PostGIS geometry and geography spatial types and functions
postgis_raster | 3.1.4 | public | PostGIS raster types and functions
(3 rows)
现在一切都根据文档进行了设置。但是,当我运行迁移时,出现以下错误:
Operations to perform:
Apply all migrations: admin, apiserver, apiserver_v2, auth, authtoken, contenttypes, enter, guardian, sessions, sites
Running migrations:
Applying apiserver_v2.0000_postgis... OK
Applying apiserver_v2.0001_initial...Traceback (most recent call last):
File "/api-server/manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python3.9/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python3.9/site-packages/django/core/management/__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python3.9/site-packages/django/core/management/base.py", line 330, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python3.9/site-packages/django/core/management/base.py", line 371, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python3.9/site-packages/django/core/management/base.py", line 85, in wrapped
res = handle_func(*args, **kwargs)
File "/usr/local/lib/python3.9/site-packages/django/core/management/commands/migrate.py", line 243, in handle
post_migrate_state = executor.migrate(
File "/usr/local/lib/python3.9/site-packages/django/db/migrations/executor.py", line 117, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/usr/local/lib/python3.9/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/usr/local/lib/python3.9/site-packages/django/db/migrations/executor.py", line 227, in apply_migration
state = migration.apply(state, schema_editor)
File "/usr/local/lib/python3.9/site-packages/django/db/migrations/migration.py", line 124, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/usr/local/lib/python3.9/site-packages/django/db/migrations/operations/models.py", line 92, in database_forwards
schema_editor.create_model(model)
File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/schema.py", line 322, in create_model
sql, params = self.table_sql(model)
File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/schema.py", line 159, in table_sql
definition, extra_params = self.column_sql(model, field)
File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/schema.py", line 212, in column_sql
db_params = field.db_parameters(connection=self.connection)
File "/usr/local/lib/python3.9/site-packages/django/db/models/fields/__init__.py", line 717, in db_parameters
type_string = self.db_type(connection)
File "/usr/local/lib/python3.9/site-packages/django/contrib/gis/db/models/fields.py", line 105, in db_type
return connection.ops.geo_db_type(self)
AttributeError: 'DatabaseOperations' object has no attribute 'geo_db_type'
特别说明:我已经查看了我能找到的所有导致此错误的问题,但我找不到任何关于该错误实际含义的解释。为什么要查找此属性,我如何验证它是否具有此属性?什么包应该放在那里以及如何放?如果我能发现这一点,诊断和解决这个问题就会容易得多。
更新:我用一个干净的 postgis 数据库尝试了这个并得到了同样的错误。我还尝试了pg_dumped 和pg_restored 数据库。没有运气。
【问题讨论】:
-
我已经尝试过了,即使使用 Docker (github.com/DusanMadar/geodjango-tutorial-kickoff) 也能顺利运行。 WRT
geo_db_type,检查stackoverflow.com/q/48895257/4183498。我将验证 Django 是否通过python manage.py dbshell连接到您期望的数据库。另外,您可以分享您的模型以查看 apiserver_v2.0001_initial 中运行的内容吗? -
可能是因为我从使用
postgres/postgres:10切换到了postgis? -
可能。 github.com/django/django/blob/main/django/contrib/gis/db/… 表示数据库后端无法识别地理数据列的类型。当我切换到
postgres:11.1-alpine时,我在运行迁移时收到django.db.utils.OperationalError: could not open extension control file "/usr/local/share/postgresql/extension/postgis.control": No such file or directory。 -
我已经设法重现了您在使用 postgis DB 时遇到的相同错误,但使用
django.db.backends.postgresql作为 DB 引擎(在DATABASES["default"]中)。难道你没有多个 settings.py 文件覆盖不同环境的 DATABASES 设置吗?
标签: python django docker postgis geodjango