I am hooking up to an existing Postgres database with Django, using inspectdb to generate my models.py file. The tables in the Postgres were created like this:
CREATE TABLE "a"
(
"id" uuid NOT NULL DEFAULT '00000000-0000-0000-0000-000000000000'::uuid,
CONSTRAINT "PK.a" PRIMARY KEY ("id")
)
CREATE TABLE "c"
(
"id" uuid NOT NULL DEFAULT '00000000-0000-0000-0000-000000000000'::uuid,
CONSTRAINT "PK.c" PRIMARY KEY ("id")
)
CREATE TABLE "b"
(
"aid" uuid NOT NULL DEFAULT '00000000-0000-0000-0000-000000000000'::uuid,
"cid" uuid NOT NULL DEFAULT '00000000-0000-0000-0000-000000000000'::uuid,
CONSTRAINT "PK.b" PRIMARY KEY ("aid","cid"),
CONSTRAINT "FK_a" FOREIGN KEY ("aid")
REFERENCES "a" ("id") MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE CASCADE,
CONSTRAINT "FK_c" FOREIGN KEY ("cid")
REFERENCES "c" ("id") MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE CASCADE
)
I then run python manage.py inspectdb --database primary > models_test.py
, which results in the following models_test.py.
class A(models.Model):
id = models.UUIDField(primary_key=True)
class Meta:
managed = False
db_table = 'a'
class B(models.Model):
aid = models.OneToOneField(A, models.DO_NOTHING, db_column='aid', primary_key=True)
cid = models.ForeignKey('C', models.DO_NOTHING, db_column='cid')
class Meta:
managed = False
db_table = 'b'
unique_together = (('aid', 'cid'),)
class C(models.Model):
id = models.UUIDField(primary_key=True)
class Meta:
managed = False
db_table = 'c'
note the OneToOneField defined on aid
.
If I instead create table b as:
CREATE TABLE "b"
(
"aid" uuid NOT NULL DEFAULT '00000000-0000-0000-0000-000000000000'::uuid,
"cid" uuid NOT NULL DEFAULT '00000000-0000-0000-0000-000000000000'::uuid,
CONSTRAINT "PK.b" PRIMARY KEY ("cid","aid"),
CONSTRAINT "FK_a" FOREIGN KEY ("aid")
REFERENCES "a" ("id") MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE CASCADE,
CONSTRAINT "FK_c" FOREIGN KEY ("cid")
REFERENCES "c" ("id") MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE CASCADE
)
then rerun inspectdb I get the following output:
class A(models.Model):
id = models.UUIDField(primary_key=True)
class Meta:
managed = False
db_table = 'a'
class B(models.Model):
aid = models.ForeignKey(A, models.DO_NOTHING, db_column='aid')
cid = models.OneToOneField('C', models.DO_NOTHING, db_column='cid', primary_key=True)
class Meta:
managed = False
db_table = 'b'
unique_together = (('cid', 'aid'),)
class C(models.Model):
id = models.UUIDField(primary_key=True)
class Meta:
managed = False
db_table = 'c'
note the OneToOneField is now on cid
. I suspect this is a bug, but I am inexperienced so wanted to ask here before reporting.
Secondary question: if this is a bug, is it worth reporting? Maybe the database design is very poor or uncommon?