Hi All,
I’ve stumbled across an issue in my web application and I’m not sure whether this is a bug or a fault of my own.
Let’s start with some models (these only include the relevant attributes for brevity).
# in django app called users
class Contributor(models.Model):
uuid = models.UUIDField(primary_key=True, default=uuid.uuid4)
name = models.CharField(max_length=512)
user = models.ForeignKey(CustomUser, null=True, on_delete=models.CASCADE)
university = models.ForeignKey(University, null=True, on_delete=models.CASCADE)
def __str__(self):
return self.name
# in django app called cases
class Case(models.Model):
uuid = models.UUIDField(primary_key=True, default=uuid.uuid4)
contributing_person = models.ForeignKey(Contributor, on_delete=models.SET_NULL, null=True)
It would appear that the necessary migrations have been made (again, edited for brevity)
# users app migration
operations = [
migrations.CreateModel(
name="Contributor",
fields=[
("uuid", models.UUIDField(default=uuid.uuid4, primary_key=True, serialize=False)),
("name", models.CharField(max_length=512)),
(
"university",
models.ForeignKey(
null=True,
on_delete=django.db.models.deletion.CASCADE,
to="users.university",
),
),
(
"user",
models.ForeignKey(
null=True,
on_delete=django.db.models.deletion.CASCADE,
to=settings.AUTH_USER_MODEL,
),
),
],
),
]
# cases app migration
operations = [
migrations.AlterField(
model_name="case",
name="contributing_person",
field=models.ForeignKey(
null=True, on_delete=django.db.models.deletion.SET_NULL, to="users.contributor"
),
),
]
When I create a Case
object where contributing_person = None
the everything runs as expected. However, when I create a Case
where contributing_person
points to a User
object, I get the following exception.
column cases_case.contributing_person does not exist
LINE 1: ...eography_id", "cases_case"."contributing_uni_id", "cases_cas...
^
HINT: Perhaps you meant to reference the column "cases_case.contributing_person_id".
I dove into my database to take a look at my cases_case
table to see what the column was called.
my_database=# select contributing_person_id from cases_case;
79b1b921-0668-4394...
79b1b921-0668-4394...
my_database=# select contributing_person from cases_case;
ERROR: column "contributing_person" does not exist
LINE 1: select contributing_person from cases_case;
^
HINT: Perhaps you meant to reference the column "cases_case.contributing_person_id".
As expected, there is no contributing_person
column, but rather a contributing_person_id
column.
What I find strange is that there is data in the table which looks to be sane. However, when I try to add some data now, I get the above exception.
I have had some migration issues which I think resulted from some duplicate files resulting from a git operation. I had a file called app_migration 2.py
which made its way into my database. I updated the row in the django_migrations
table to point to the original file app_migration.py
. I reverted to a previous commit before I had deleted the app_migration 2.py
and compared it with app_migration.py
. They are identical.
I does strike me as odd, after taking a glance at the django source code (I’m not saying it was at all comprehensable for my noggin) but I did notice that the models.ForeignKey
object create FKs column names in the DB with an _id
appended to the model’s attribute’s name.
I suppose what I am getting at is why is Django trying to insert something into contributing_person
column when it appears it should actually be contributing_person_id
? From where is Django getting the contributing_person
column name? I’d like to work out whether this is my fault, and if so, how to fix it, or if it is a Django bug so I can go and report it.
Any help as always is warmly received. Thank you.
Cheers,
Conor