SectorLabs/django-postgres-extra

Unable to use GeoDjango features

rifatrakib opened this issue · 1 comments

I am trying to use this extension with some models where I have geospatial fields and my database has all the necessary extensions needed to support GeoDjango. Using the django.contrib.gis.db.backends.postgis database engine works perfectly fine, but when I was trying to use the psqlextra.backend engine, I was getting the following error:

File "E:\github-rebuild\comfy-homes\manage.py", line 35, in <module>
    main()
  File "E:\github-rebuild\comfy-homes\manage.py", line 31, in main
    execute_from_command_line(sys.argv)
  File "E:\github-rebuild\comfy-homes\housing-env\lib\site-packages\django\core\management\__init__.py", line 446, in execute_from_command_line
    utility.execute()
  File "E:\github-rebuild\comfy-homes\housing-env\lib\site-packages\django\core\management\__init__.py", line 440, in execute       
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "E:\github-rebuild\comfy-homes\housing-env\lib\site-packages\django\core\management\base.py", line 414, in run_from_argv     
    self.execute(*args, **cmd_options)
  File "E:\github-rebuild\comfy-homes\housing-env\lib\site-packages\django\core\management\base.py", line 460, in execute
    output = self.handle(*args, **options)
  File "E:\github-rebuild\comfy-homes\housing-env\lib\site-packages\django\core\management\base.py", line 98, in wrapped
    res = handle_func(*args, **kwargs)
  File "E:\github-rebuild\comfy-homes\housing-env\lib\site-packages\django\core\management\commands\migrate.py", line 290, in handle    post_migrate_state = executor.migrate(
  File "E:\github-rebuild\comfy-homes\housing-env\lib\site-packages\django\db\migrations\executor.py", line 131, in migrate
    state = self._migrate_all_forwards(
  File "E:\github-rebuild\comfy-homes\housing-env\lib\site-packages\django\db\migrations\executor.py", line 163, in _migrate_all_forwards
    state = self.apply_migration(
  File "E:\github-rebuild\comfy-homes\housing-env\lib\site-packages\django\db\migrations\executor.py", line 248, in apply_migration 
    state = migration.apply(state, schema_editor)
  File "E:\github-rebuild\comfy-homes\housing-env\lib\site-packages\django\db\migrations\migration.py", line 131, in apply
    operation.database_forwards(
  File "E:\github-rebuild\comfy-homes\housing-env\lib\site-packages\django\db\migrations\operations\models.py", line 93, in database_forwards
    schema_editor.create_model(model)
  File "E:\github-rebuild\comfy-homes\housing-env\lib\site-packages\psqlextra\backend\schema.py", line 69, in create_model
    super().create_model(model)
  File "E:\github-rebuild\comfy-homes\housing-env\lib\site-packages\django\db\backends\base\schema.py", line 429, in create_model   
    sql, params = self.table_sql(model)
  File "E:\github-rebuild\comfy-homes\housing-env\lib\site-packages\django\db\backends\base\schema.py", line 209, in table_sql      
    definition, extra_params = self.column_sql(model, field)
  File "E:\github-rebuild\comfy-homes\housing-env\lib\site-packages\django\db\backends\base\schema.py", line 338, in column_sql     
    db_params = field.db_parameters(connection=self.connection)
  File "E:\github-rebuild\comfy-homes\housing-env\lib\site-packages\django\db\models\fields\__init__.py", line 808, in db_parameters    type_string = self.db_type(connection)
  File "E:\github-rebuild\comfy-homes\housing-env\lib\site-packages\django\contrib\gis\db\models\fields.py", line 122, in db_type   
    return connection.ops.geo_db_type(self)
AttributeError: 'PostgresOperations' object has no attribute 'geo_db_type'

Before adding the following model, I could use the engine perfectly fine when there were no geospatial fields in any of my models. After adding these 3 models, everything started to go wrong. Here are the models:

from django.utils.translation import gettext_lazy as _
from django.contrib.gis.db import models as gis_models
from django.db import models


class County(models.Model):
    county_number = models.IntegerField(primary_key=True)
    county_name = models.CharField(_('county'), max_length=50)
    label = models.CharField(_('label for county'), max_length=50, blank=True)
    boundary = gis_models.PolygonField()


class City(models.Model):
    city_number = models.IntegerField(primary_key=True)
    city_name = models.CharField(_('city'), max_length=50)
    label = models.CharField(_('label for city'), max_length=50, blank=True)
    county = models.ForeignKey(County, on_delete=models.CASCADE)
    boundary = gis_models.PolygonField()


class PostalCode(models.Model):
    postal_code = models.IntegerField(primary_key=True)
    postal_location = models.CharField(_('postal location'), max_length=50)
    label = models.CharField(_('label for postal code'), max_length=50, blank=True)
    city = models.ForeignKey(City, on_delete=models.CASCADE)
    boundary = gis_models.PolygonField()

I guess if you could keep all the defaults from what Django provides for GIS libraries, that would be perfect! Looking forward to getting all the awesome PostGIS features with this module too,

Making the custom database back-end work for example PostGIS is well documented here: https://django-postgres-extra.readthedocs.io/en/latest/settings.html#postgres-extra-db-backend-base

There was some buginess due to recent changes in Django, but they have been addressed in #179 and released as part of v2.0.6.