Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm switching from a MySQL backend to a PostgreSQL backend and am running into some model inheritance issues. Here is an example of the models:

class Parent(models.Model):
   key = models.Charfield(...)
   value = models.Charfield(...)
   content_type = models.ForeignKey(ContentType)
   object_id = models.CharField(max_length=200)
   content_object = generic.GenericForeignKey('content_type', 'object_id')

class Child1(Parent):
   pass 

class Child2(Parent):
   pass

The reason we have two child classes like this is, we're simulating two key/value pairs in another model, and wanted to separated them into two tables for an easier lookup. The Generic FKs were also for attaching this to other models. This inheritance setup works fine in MySQL, but when I switched it to PostgreSQL, I get an error when trying to run our tests (but syncdb works fine). It's as if Django is OK with the relationship but PostgreSQL doesn't like the SQL being generated. When I look at what's being generated from syncdb I see this:

CREATE TABLE "myapp_parent" (
"id" serial NOT NULL PRIMARY KEY,
"key" varchar(200) NOT NULL,
"value" varchar(200) NOT NULL,
"content_type_id" integer NOT NULL REFERENCES "django_content_type" ("id") DEFERRABLE  INITIALLY DEFERRED,
"object_id" varchar(200) NOT NULL);

CREATE TABLE "myapp_child1" (
"parent_ptr_id" integer NOT NULL PRIMARY KEY REFERENCES "myapp_parent" ("id") DEFERRABLE INITIALLY DEFERRED);

CREATE TABLE "myapp_child2" (
"parent_ptr_id" integer NOT NULL PRIMARY KEY REFERENCES "myapp_parent" ("id")    DEFERRABLE INITIALLY DEFERRED);

So everything looks right, then when I run my tests I get this:

Error: Database test_myapp 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: column "id" of relation "myapp_child1" does not exist

When I run flush:

SELECT setval(pg_get_serial_sequence('"myapp_child1"','id'), 1, false);

I've tried manually adding an ID field as the primary key in the child model but Django throws an error saying it conflicts with the Parent's ID field. How do I fix this so PostgreSQL likes it? And thanks in advance.

share|improve this question
When you use MySQL, does Django create a column named "id" in table "myapp_child1"? – Mike Sherrill 'Catcall' Jan 2 at 17:09
No it does not. In both DBs the only column is parent_ptr_id – user994013 Jan 2 at 18:36
Sounds like a Django bug. I looked briefly through their ticketing system, but I didn't see it. Maybe you should report it. (But wait a bit to see if anyone else here on SO might know more than I do.) – Mike Sherrill 'Catcall' Jan 2 at 19:26
Read @danodonovan doc link, for Primary key type compatibility – catherine Jan 29 at 15:11

2 Answers

If you're using model inheritance in django, you should declare class Parent to be abstract

class Parent(models.Model):
    ...
    class Meta:
        abstract = True

See the docs. I imagine that some postgres / mysql differences have only been tested against standard conforming code - which could be why you're having problems here. I'd also recommend ./manage.py syncdb after making these changes ;-)

If in doubt, and on in a testing environment, you can drop your tables and start again with

$ ./manage.py sqlclear | ./manage.py dbshell
share|improve this answer

Your model must contain one - and only one - foreign key to the target model. If you have more than one foreign key, a validation error will be raised. This is one of the restrictions of django.

share|improve this answer
"The "object_id" field doesn't have to be the same type as the primary key fields on the related models, but their primary key values must be coercible to the same type as the "object_id" field by its get_db_prep_value() method." – catherine Jan 29 at 15:08
Read the Primary key type compatibility below of that docs – catherine Jan 29 at 15:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.