Getting 'DatabaseOperations' object has no attribute 'geo_db_type' error when doing a syncdb

I'm attempting to run heroku run python manage.py syncdb on my GeoDjango app on Heroku, but I get the following error:

AttributeError: 'DatabaseOperations' object has no attribute 'geo_db_type'

所有of my research都产生了相同的解决方案:确保使用django.contrib.gis.db.backends.postgis作为数据库引擎。有趣的是I'm already doing this(我在INSTALLED_APPS中也有django.contrib.gis):

settings.py


DATABASES = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': '...',
'HOST': '...',
'PORT': ...,
'USER': '...',
'PASSWORD': '...'
}
}


INSTALLED_APPS = (
...,
'django.contrib.gis',
)

我还漏掉了什么吗?非常感谢任何帮助,以下是完整的错误跟踪供参考:

Running `python manage.py syncdb` attached to terminal... up, run.1
Creating tables ...
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_user_permissions
Creating table auth_user_groups
Creating table auth_user
Creating table django_content_type
Creating table django_session
Creating table django_site
Creating table django_admin_log
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/app/lib/python2.7/site-packages/django/core/management/__init__.py", line 443, in execute_from_command_line
utility.execute()
File "/app/lib/python2.7/site-packages/django/core/management/__init__.py", line 382, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/app/lib/python2.7/site-packages/django/core/management/base.py", line 196, in run_from_argv
self.execute(*args, **options.__dict__)
File "/app/lib/python2.7/site-packages/django/core/management/base.py", line 232, in execute
output = self.handle(*args, **options)
File "/app/lib/python2.7/site-packages/django/core/management/base.py", line 371, in handle
return self.handle_noargs(**options)
File "/app/lib/python2.7/site-packages/django/core/management/commands/syncdb.py", line 91, in handle_noargs
sql, references = connection.creation.sql_create_model(model, self.style, seen_models)
File "/app/lib/python2.7/site-packages/django/db/backends/creation.py", line 44, in sql_create_model
col_type = f.db_type(connection=self.connection)
File "/app/lib/python2.7/site-packages/django/contrib/gis/db/models/fields.py", line 200, in db_type
return connection.ops.geo_db_type(self)
AttributeError: 'DatabaseOperations' object has no attribute 'geo_db_type'

更新:我遵循了Geodjango教程Heroku/Django教程,并建立了一个简单的应用程序,在我的开发机器上工作。我使用自定义GeoDjango BuildPack将其推送到Heroku,并尝试SyncDB,但得到相同的错误。这是Django/Geodjango、Heroku或BuildPack的问题吗?我的开发环境使用的是PostgreSQL 9.1和PostGIS 2.0,但Heroku使用的是9.0.9和1.5,这可能是问题所在吗?

43406 次浏览

The buildpack was the primary culprit here. Instead of using the GeoDjango buildpack listed on Heroku's buildpack page, I used one of it's forks that was updated more recently.

Also, when I do a git push heroku master, Heroku creates a dev database for the app, and when I do a syncdb, my DATABASES setting is ignored and Heroku tries to use the dev database instead... obviously an issue, because dev databases don't/can't have PostGIS installed. So I destroyed the dev database after it was created with the git push (with the correct buildpack), then ran syncdb and it works.

The OP was using the GeoDjango buildpack, but in case anyone gets here using Geo buildpack and dj_database_url like I was, in settings.py don't forget the last line:

import dj_database_url
DATABASES['default'] = dj_database_url.config()
DATABASES['default']['ENGINE'] = 'django.contrib.gis.db.backends.postgis'

UPDATE

dj_database_url directly supports PostGIS. You can do without the last line in the code above if you can change your database URL to start with postgis.

This post is old but I just wanted to share my answer to this problem. I'm using the Dj Database package, and I didn't know that the connection URL is different when using PostGIS. The connection string for PostGIS is postgis://USER:PASSWORD@HOST:PORT/NAME

Hope this helps someone.

I got this error when trying to run tests with the test db set like so:

if 'test' in sys.argv:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': '_testdb',
}
}

The problem is that the sqlite3 DatabaseOperations object doesn't have the attribute geo_db_type (like the title of this post suggests).

My solution was to change the backend to the sqlite equivalent GIS engine:

        'ENGINE': 'django.contrib.gis.db.backends.spatialite'

See the Django docs on GeoDjango installation for all the possible backends, with installation instructions: https://docs.djangoproject.com/en/3.0/ref/contrib/gis/install/#spatial-database

In may case I was having this issue because I forgot to comment out the db settings further down in settings.py:

# Update database configuration with $DATABASE_URL.
db_from_env = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(db_from_env)

These lines were overriding the settings that I had added above. Adding this in case someone else had the same issue.

for me helped

1) add 'django.contrib.gis', to INSTALLED_APPS
2) change from

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',

to

DATABASES = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.mysql',

I'm using the Python sample application on stack cedar 14 and the regular Heroku buildpack Heroku/python with PostGIS and had the same problem that my database settings were overwritten with the wrong DB engine, which caused heroku run python manage.py migrate to fail with the above error. Just adding the engine in the settings would not change anything. After some investigation I found out that it is the call to django_heroku.settings(locals()) in the last line of my settings.py which is reverting my changes.

I fixed it by overwriting the engine again like this by adding a line afterwards:

django_heroku.settings(locals())
DATABASES['default']['ENGINE'] = 'django.contrib.gis.db.backends.postgis'

I was having the same problem and I had to change:

'ENGINE': 'django.db.backends.postgresql_psycopg2',

to:

'ENGINE': 'django.contrib.gis.db.backends.postgis',

I changed my default db engine from psycopg2 to postgis

PREVIOUSLY

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
...,
}
}

NOW

DATABASES = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
...,
}
}

Settings.py > INSTALLED_APPS

INSTALLED_APPS = [
.
.
'django.contrib.gis'
.
]

Settings.py > Databases

DATABASES = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.mysql',
...,
}
}