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

牧云@^-^@ 提交于 2019-11-27 07:56:48

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.

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',
        }
    }

Problem being 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/1.9/ref/contrib/gis/install/#spatial-database

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 was having the same problem and I had to change:

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

to:

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

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', 

In python3 I'm getting the same error:

  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 673, in db_parameters
    type_string = self.db_type(connection)
  File "/app/.heroku/python/lib/python3.6/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'

I have properly used dj-database-url to set the engine, however I'm still seeing the error

I added a print statement to output my db setups as they're being interpreted by py

In settings.py:

if os.getenv('DYNO'):
    GDAL_LIBRARY_PATH = os.path.expandvars(os.getenv('GDAL_LIBRARY_PATH'))
    GEOS_LIBRARY_PATH = os.path.expandvars(os.getenv('GEOS_LIBRARY_PATH'))
    DATABASES['default'] =  dj_database_url.parse(os.getenv('DATABASE_URL'),'django.contrib.gis.db.backends.postgis')
    print(DATABASES['default'])

Here is the print statement outputting what the heroku server is interpreting as my DATABASES['default'] credentials. It appears the engine is properly being set.

{'NAME': 'name', 'USER': 'usr', 'PASSWORD': 'pw', 'HOST': 'herokuec2host.amazonaws.com', 'PORT': 5432, 'CONN_MAX_AGE': 0, 'ENGINE': 'django.contrib.gis.db.backends.postgis'}

I'm using the Python sample application on stack cedar 14 and the regular Heroko 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 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 if 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 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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!