I am switching from a MySQL backend to a PostgreSQL backend and run into some model inheritance issues. Here is an example of 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
We have two child classes, such as: we model two key / value pairs in another model and would like to divide them into two tables for a more convenient search. Generic FKs were also designed to connect to other models. This inheritance setting works fine in MySQL, but when I switch it to PostgreSQL, I get an error when I try to run our tests (but syncdb works fine). It is as if Django is fine with the relationship, but PostgreSQL does not like the generated SQL. When I look at what is generated from syncdb, I see the following:
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 the following:
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 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 tried to manually add the identifier field as the primary key in the child model, but Django throws an error saying that it conflicts with the parent id field. How to fix it so PostgreSQL likes it? And thanks in advance.