How do I convert an existing column to Foreign Key?

9 views
Skip to first unread message

Tom Tanner

unread,
Jan 30, 2018, 12:02:52 AM1/30/18
to Django users
I have two models `model_a` and `model_b`, where their tables look something like this...


Table `a` looks something like this -- first row is column names, both contain characters:

    id |  tractce   | someString
    2  |  "0011900" | "Label here"

Table `b`:

    id | tractFIPS
   
1  | "0011900"



Here's code from `models.py`:

    class model_a(models.Model):
    tractce
= models.CharField(max_length=6)
    someString
= models.TextField()


   
class model_b(models.Model):
    tractFIPS
= models.CharField(max_length=6)



I want to convert `model_b` so that the `tractFIPS` field is a foreign key referencing `model_a`. Something like this I guess...

    class model_b(models.Model):
    tractFIPS_FK
= models.ForeignKey(model_a)



And the table for `model_b` would look something like this in the end:

    id |  tractFIPS_FK_id
   
1  |  2


How do I convert `model_b`'s `tractFIPS` field to a foreign key with the correct values in them? 
Reply all
Reply to author
Forward
0 new messages