'GeoDjango heroku 'DatabaseOperations' object has no attribute 'spatial_aggregate_name'
I'm trying to deploy my app on Heroku, which works locally but is throwing this error when deployed on heroku:
AttributeError : 'DatabaseOperations' object has no attribute 'spatial_aggregate_name'
For context I the api call that is triggering the error is trying to return all addresses from a table that are in a certain boundary.
my DATABASE settings in settings.py:
INSTALLED_APPS = [
'map.apps.MapConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.postgres',
'rest_framework',
'django.contrib.gis',
'coreapi',
'listings',
'drf_yasg',
]
DATABASES = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': 'neighborhoods',
'USER': 'django',
'PASSWORD': db_pass,
'HOST':'db_name.xxxxxxx.us-west-1.rds.amazonaws.com',
'PORT':'5432'
}
}
# Heroku: Update database configurations from $DATABASE_URL.
import dj_database_url
db_from_env = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(db_from_env)
# Activate Django-Heroku.
django_heroku.settings(locals(), staticfiles=False)
I'm using the heroku goe buildpack.
I just can't seem to figure out why it is working locally but not on heroku.
Here is my traceback:
> Django Version: 4.0.2 Python Version: 3.9.4 Installed Applications:
> ['map.apps.MapConfig', 'django.contrib.admin',
> 'django.contrib.auth', 'django.contrib.contenttypes',
> 'django.contrib.sessions', 'django.contrib.messages',
> 'django.contrib.staticfiles', 'django.contrib.postgres',
> 'rest_framework', 'django.contrib.gis', 'coreapi', 'listings',
> 'drf_yasg'] Installed Middleware:
> ['django.middleware.security.SecurityMiddleware',
> 'whitenoise.middleware.WhiteNoiseMiddleware',
> 'django.contrib.sessions.middleware.SessionMiddleware',
> 'django.middleware.common.CommonMiddleware',
> 'django.middleware.csrf.CsrfViewMiddleware',
> 'django.contrib.auth.middleware.AuthenticationMiddleware',
> 'django.contrib.messages.middleware.MessageMiddleware',
> 'django.middleware.clickjacking.XFrameOptionsMiddleware']
>
>
>
> Traceback (most recent call last): File
> "/app/.heroku/python/lib/python3.9/site-packages/django/core/handlers/exception.py",
> line 47, in inner
> response = get_response(request) File "/app/.heroku/python/lib/python3.9/site-packages/django/core/handlers/base.py",
> line 181, in _get_response
> response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/app/.heroku/python/lib/python3.9/site-packages/django/views/decorators/csrf.py",
> line 54, in wrapped_view
> return view_func(*args, **kwargs) File "/app/.heroku/python/lib/python3.9/site-packages/django/views/generic/base.py",
> line 69, in view
> return self.dispatch(request, *args, **kwargs) File "/app/.heroku/python/lib/python3.9/site-packages/rest_framework/views.py",
> line 509, in dispatch
> response = self.handle_exception(exc) File "/app/.heroku/python/lib/python3.9/site-packages/rest_framework/views.py",
> line 469, in handle_exception
> self.raise_uncaught_exception(exc) File "/app/.heroku/python/lib/python3.9/site-packages/rest_framework/views.py",
> line 480, in raise_uncaught_exception
> raise exc File "/app/.heroku/python/lib/python3.9/site-packages/rest_framework/views.py",
> line 506, in dispatch
> response = handler(request, *args, **kwargs) File "/app/.heroku/python/lib/python3.9/site-packages/rest_framework/decorators.py",
> line 50, in handler
> return func(*args, **kwargs) File "/app/listings/views.py", line 62, in GetHomesInHood
> home_recs = find_homes( File "/app/listings/HomeFinder.py", line 205, in find_homes
> filtered_listings = filter_listings_model(hood_ids, beds, baths) File "/app/listings/HomeFinder.py", line 40, in filter_listings_model
> hood_boundaries = NeighborhoodBoundaries.objects.filter(pk__in=neighborhood_ids).aggregate(Collect('geometry'))
> File
> "/app/.heroku/python/lib/python3.9/site-packages/django/db/models/query.py",
> line 403, in aggregate
> return query.get_aggregation(self.db, kwargs) File "/app/.heroku/python/lib/python3.9/site-packages/django/db/models/sql/query.py",
> line 501, in get_aggregation
> result = compiler.execute_sql(SINGLE) File "/app/.heroku/python/lib/python3.9/site-packages/django/db/models/sql/compiler.py",
> line 1189, in execute_sql
> sql, params = self.as_sql() File "/app/.heroku/python/lib/python3.9/site-packages/django/db/models/sql/compiler.py",
> line 531, in as_sql
> extra_select, order_by, group_by = self.pre_sql_setup() File "/app/.heroku/python/lib/python3.9/site-packages/django/db/models/sql/compiler.py",
> line 59, in pre_sql_setup
> self.setup_query() File "/app/.heroku/python/lib/python3.9/site-packages/django/db/models/sql/compiler.py",
> line 50, in setup_query
> self.select, self.klass_info, self.annotation_col_map = self.get_select() File
> "/app/.heroku/python/lib/python3.9/site-packages/django/db/models/sql/compiler.py",
> line 267, in get_select
> sql, params = self.compile(col) File "/app/.heroku/python/lib/python3.9/site-packages/django/db/models/sql/compiler.py",
> line 463, in compile
> sql, params = node.as_sql(self, self.connection) File "/app/.heroku/python/lib/python3.9/site-packages/django/contrib/gis/db/models/aggregates.py",
> line 25, in as_sql
> function=function or connection.ops.spatial_aggregate_name(self.name),
>
> Exception Type: AttributeError at /listings/neighborhood/ Exception
> Value: 'DatabaseOperations' object has no attribute
> 'spatial_aggregate_name'
I just can't seem to pinpoint what is changing about the heroku setup and why it is not finding necessary attributes like 'spacial_aggregate_name'. Any help is greatly appreciated :)
Solution 1:[1]
PostGIS is not enabled on Heroku Postgres databases by default. You need to enable it by running
CREATE EXTENSION postgis;
on your database before you can use it.
See the documentation for more details.
Solution 2:[2]
This may be a patch rather than a solution, but I am able to get it to work if I make the following adjustments in settings.py. Commenting out the following settings:
# Heroku: Update database configurations from $DATABASE_URL.
#import dj_database_url
# db_from_env = dj_database_url.config(conn_max_age=500,
# engine = 'django.contrib.gis.db.backends.postgis',
# )
# DATABASES['default'].update(db_from_env)
# Activate Django-Heroku.
# django_heroku.settings(locals(), staticfiles=False)
it seems these lines were changing the settings in heroku to use a different postgresql instance setup via heroku-postgres instead of the database I already had setup properly.
Hopefully there is no need for those lines.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Chris |
| Solution 2 | jamjahal |
