﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
16160	GeoDjango syncdb fails if using post_syncdb signal on postgis to access geometry columns	petr.gorodechnyj@…	nobody	"I use geojango on Postgresql 8.4 with the help of postgis 1.5. When I create model with geo field and use it as is - that's ok. But in my case I need to do some serious stuff on DB level so I created couple of postgresql sql-functions. So I wrote sql and execute it in post_syncdb signal handler.

models.py:
{{{
from django.contrib.gis.db import models

class Shop(models.Model):
    ....
    point               = models.PointField(null=True, spatial_index=False)
}}}

management.py:
{{{
from django.db.models.signals import post_syncdb
def post_syncdb_task(sender, **kwargs):
    from django.db import connection
    cursor = connection.cursor()
    sql =   '''CREATE OR REPLACE FUNCTION functionname(integer, text) RETURNS double precision AS $$ 
                SELECT ST_distance_sphere(point, ST_GeomFromEWKT($2)) as d 
                FROM this_package_shop 
                WHERE this_package_shop.id = $1;
            $$ LANGUAGE SQL;'''
    cursor.execute(sql);
from this_package import models as this_models
post_syncdb.connect(post_syncdb_task, sender=this_models)
}}}

When I execute syncdb on non-existing table, e.g. in python manage.py test I get:
{{{
  File ""/home/gorodechnyj/workspace/projectname/this_package/management.py"", line 18, in post_syncdb_task
    cursor.execute(sql);
  File ""/usr/lib/python2.7/dist-packages/django/db/backends/postgresql_psycopg2/base.py"", line 44, in execute
    return self.cursor.execute(query, args)
django.db.utils.DatabaseError: column ""point"" does not exist
LINE 2:                 SELECT ST_distance_sphere(point, ST_GeomFrom...
}}}

And most peculiar thing: there's really no trace of field point at all! It is simply not being created.
If I disable my post_syncdb handler, error dissapears and field ""point"" is being created but not sql functions. 
So no testing on code which use them can be performed."	New feature	closed	GIS	1.3	Normal	duplicate	geodjango postgresql post_syncdb		Unreviewed	0	0	0	0	0	0
