0

This is still in dev, so I am relatively flexible in how to approach this.

So I'm importing data (via fixtures, the import works fine) from an existing database into a django application. The source data is not uniformed in how it manages ID & primary keys. Some tables use what seems to be an auto-increment (similar to what django would produce by default). Others use some sort of integers. The relationships in the data dump are established based on those fields. Seems I can keep on using auto-increments in all cases.

They are conveniently not named uniformly: id, pk, pk_sometablename, etc.

The fixtures I use to import look like this (I generated them using a script based on the datadump, so this can be changed if needs be):

{
    "model": "admin_account.client",
    "pk": "168",
    "fields":
    {
        "pk_client": "168",

My django model:

class Client(models.Model):
    pk_client = models.IntegerField(verbose_name='Pk_client', blank=True, null=True)

I need to be able to import the data in such a way that this field, the pk_client field is used as the primary key (it can still remain as an auto-increment). So I tried to change to this:

class Client(models.Model):
    pk_client = models.AutoField(primary_key=True, verbose_name="pk_client", default=-9999)

However if I try this migration with my currently populated dev DB, I get an error:

django.db.utils.OperationalError: foreign key mismatch - "purchase_orders_apent" referencing "admin_client"

I assume django complains because the apent table used to try to lookup client.id, and since I know tell django to use pk_client as the primary key, perhaps those tables referencing it are now unable to find their match). But there's a lot of tables involved.

What the easiest way to manage this?

Should I completely define those models with an empty DB (e.g. define the AutoFields for each model, assign the old db auto-increment value to that same field, and only THEN import the data)?

Or is there something I should change in my fixture definition/the way I import the data?

logicOnAbstractions
  • 2,178
  • 4
  • 25
  • 37
  • Don't you need to define the [to_field arg](https://docs.djangoproject.com/en/3.2/ref/models/fields/#django.db.models.ForeignKey.to_field) of your Foreign key field, in the model representing your `apent` table? – Lotram Jul 23 '21 at 15:57
  • Perhaps - although if I am setting primary_key=True, and the doc says that the primary_key=True can only be set on a single field per model, then... shouldn't the foreign_key work anyhow? – logicOnAbstractions Jul 23 '21 at 17:50
  • At any rate, if that's the case, I guess the answer would be to work upstream & create the fixtures in such as way that each model as something like id=AutoField(primary_key=True), which corresponds to whatever field the datadumps has for PK for that table. I would have to edit my fixtures producing script, but at least i wouldn't need to edit nearly every single foreign key (I have like 70 tables, so... 100+ FK I guess) – logicOnAbstractions Jul 23 '21 at 18:13

0 Answers0