PostgreSQL Django model inheritance error

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.

+4
source share
2 answers

If you use model inheritance in django, you must declare the class Parent abstract

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

See the docs . I believe that some of the differences between postgres / mysql were tested only for compliance with the code standard, and this may be the reason that you have problems. I also recommend ./manage.py syncdb after making these changes; -)

If in doubt and are in a test environment, you can leave your tables and start over with

 $ ./manage.py sqlclear | ./manage.py dbshell 
+2
source

Your model should contain one - and only one - foreign key for the target model. If you have more than one foreign key, a verification error will be confirmed. This is one of the limitations of django.

+2
source

All Articles