djangodatabasepostgresqlpsycopg2dbmigrate

InvalidTextRepresentation: Invalid input syntax for type bigint:"All Forms"


I had a field in my model with

book_classes = (("","Select Form"),("1",'F1'),("2",'F2'),("3",'F3'),("4",'F4'),("All Forms","All Forms"))
b_classes = models.CharField('Form',max_length=9,choices=book_classes,default="n/a")

And then changed it to

b_class =models.ForeignKey(ClassBooks,on_delete=models.CASCADE)

Where

class ClassBooks(models.Model):
    name = models.CharField(max_length=10)

I'm now stuck because when I try to migrate I get an error.

Invalid input syntax for type bigint:"All Forms"

Makemigrations and migrate worked well in development. When I pushed to digital ocean, the migrate returned the error stated. What do I need to do, please?


Solution

  • See Foreign Key field. By default a FK field is going to use the Primary Key of the referenced table(model), in this case the id field of ClassBooks. The id field is an integer so you get the error when trying to use a string field. To make this work, from the documentation link :

    ForeignKey.to_field

    The field on the related object that the relation is to. By default, Django uses the primary key of the related object. If you reference a different field, that field must have unique=True.

    Which in your case becomes:

    b_class =models.ForeignKey(ClassBooks,to_field='name',on_delete=models.CASCADE)
    

    This assumes that the name field has a Unique constraint on it.

    Though I am not sure how "", "1", "2" ... map to ClassBooks.name.