﻿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
12728	loadata/flush issues given GenericRelation, model inheritance and postgres	pragmar	nobody	"This issue was encountered while porting from mysql to postgres and resembles #11107, but under a entirely different set of circumstances. There appears to be something going wrong in the database operations with the combination of model inheritance and generic relations. I've boiled it down to the following example using postgres 8.4.2 and psycopg2.

''models.py''
{{{
from django.db import models
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic

class ExampleBase(models.Model):        
    title = models.CharField('title', max_length=200, blank=True, null=True)    
    created = models.DateTimeField('created', auto_now_add=True)
    modified = models.DateTimeField('modified', auto_now=True)        
    def __unicode__(self):
        return u'%s' % self.title    

class Event(ExampleBase):    
    start = models.DateTimeField('start time')
    end = models.DateTimeField('end')    
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()    
    content_object = generic.GenericForeignKey(""content_type"", ""object_id"")
    
class Location(ExampleBase):
    address = models.CharField('address', max_length=200)
    city = models.CharField('city', max_length=50)
    state = models.CharField('state', max_length=50)    
    events = generic.GenericRelation(Event)

}}}

Given those models the error can be created by (1) running manage.py syncdb and (2) then running manage.py flush:


{{{
W:\development\web\example.com\example>manage.py flush
    Type 'yes' to continue, or 'no' to cancel: yes
Error: Database example couldn't be flushed. Possible reasons:
  * The database isn't running or isn't configured correctly.
  * At least one of the expected database tables doesn't exist.
  * The SQL was invalid.
Hint: Look at the output of 'django-admin.py sqlflush'. That's the SQL this command wasn't able to run.
The full error: relation ""exampleapp_event_id_seq"" does not exist
LINE 1: SELECT setval('""exampleapp_event_id_seq""', 1, false);
}}}

An identical error can be produced by via loaddata, here is a simple fixture for the above models, no generic relation objects need be created to trigger the error so long as the relationship is defined in the models:

{{{
[
  {
    ""pk"": 1, 
    ""model"": ""exampleapp.examplebase"", 
    ""fields"": {
      ""created"": ""2010-01-29 08:27:10"", 
      ""modified"": ""2010-01-29 08:27:10"", 
      ""title"": ""test""
    }
  }, 
  {
    ""pk"": 1, 
    ""model"": ""exampleapp.location"", 
    ""fields"": {
      ""city"": ""Boston"", 
      ""state"": ""MA"", 
      ""address"": ""1 Main St.""
    }
  }
]
}}}

{{{
W:\development\web\example.com\example>manage.py loaddata test_fixture.json
Installing json fixture 'test_fixture' from absolute path.
Traceback (most recent call last):
  File ""W:\development\web\example.com\example\manage.py"", line 21, in <module>
    execute_manager(settings)
  File ""C:\Python26\lib\site-packages\django\core\management\__init__.py"", line 438, in execute_manager
    utility.execute()
  File ""C:\Python26\lib\site-packages\django\core\management\__init__.py"", line 379, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File ""C:\Python26\lib\site-packages\django\core\management\base.py"", line 195, in run_from_argv
    self.execute(*args, **options.__dict__)
  File ""C:\Python26\lib\site-packages\django\core\management\base.py"", line 222, in execute
    output = self.handle(*args, **options)
  File ""C:\Python26\lib\site-packages\django\core\management\commands\loaddata.py"", line 212, in handle
    cursor.execute(line)
psycopg2.ProgrammingError: column ""id"" does not exist
LINE 1: ... setval('""exampleapp_event_id_seq""', coalesce(max(""id""), 1),...
}}}

I threw a print statement on the output of db.backends.postgresql.operations to see the full line (below):
{{{
['SELECT setval(\'""exampleapp_examplebase_id_seq""\', coalesce(max(""id""), 1), max(""id"") IS NOT null) FROM ""exampleapp_examplebase"";',
 'SELECT setval(\'""exampleapp_event_id_seq""\', coalesce(max(""id""), 1), max(""id"") IS NOT null) FROM ""exampleapp_event"";']
}}}
Let me know if there's more information I can provide. The same model hierarchy was working under mysql, and if I change the genericrelation to inherit from models.Model it works - so the issue requires a specific set of circumstances.

Thanks."	Bug	closed	contrib.contenttypes	dev	Normal	fixed	psycopg2.ProgrammingError, GenericRelation	magma.chambers@… joel@… lorin@… timograham@…	Accepted	1	0	0	1	0	0
