Monday, January 29, 2018

How do I convert an existing column to Foreign Key?

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? 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a347bdbc-9cd7-4cda-a0f6-873812c360e1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment