Django Error – ValueError: Field 'id' expected a number but got 'company'

Question:

I’m currently writing an application in django called account, now everything was working fine (as it usually happens), but I had to add more information in my models.py file, and all of the sudden, I got problems running the migrations, certainly as django enthusiast the usual approach is to delete all the migrations in the migration folder, delete the Database and finally run the usual set of commands.

Unfurtunetely this time, that trick is not solving my problem, and I’m getting the following error:

ValueError: Field ‘id’ expected a number but got ‘company’.

That error appears when I type python manage.py migrate, the run migration runs without problems, and a migration 0001 file and a database is created, but I still get the error when running the migrate command.

models.py

from django.db import models
from django.conf import settings
from django_countries.fields import CountryField
from phone_field import PhoneField
from djmoney.models.fields import MoneyField

class Profile(models.Model):
    user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    email = models.EmailField(blank=True,null=True)
    role = models.TextField(blank=True)
    location = models.TextField(blank=True)
    photo = models.ImageField(upload_to='users/%Y/%m/%d', blank=True)

    def __str__(self):
        return self.user

class Company(models.Model):
    company = models.CharField(blank=True, max_length=30)

    def __str__(self):
        return self.company

class Client(models.Model):
    firstname = models.CharField(blank=True, max_length=30)
    lastname = models.CharField(blank=True, max_length=15)
    company = models.ForeignKey(Company, on_delete=models.CASCADE, default="company")
    position = models.CharField(blank=True, max_length=15)
    country = CountryField(blank_label='(select country)')
    email = models.EmailField(blank=True, max_length=100, default="this_is@n_example.com")
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    phone = PhoneField(default="(XX)-XXX-XXX")

    def __str__(self):
        return f'{self.firstname}'

class Leads(models.Model):
    CHOICES = (
                ('Illumination Studies','Illumination Studies'),
                ('Training','Training'),
                ('Survey Design','Survey Design'),
                ('Software License','Software License')
               )
    STATUS = (('Open','Open'),
               ('Closed','Closed'),
               ('Canceled', 'Canceled')
              )
    project_id = models.BigAutoField(primary_key=True)
    company = models.ForeignKey(Company, on_delete=models.CASCADE, default="company")
    agent = models.ForeignKey(Profile, on_delete=models.CASCADE, default="agent")
    created_at = models.DateTimeField(auto_now_add=True)
    point_of_contact = models.CharField(blank=True, max_length=30)
    expected_revenue = MoneyField(max_digits=14, decimal_places=2, default_currency='USD')
    expected_licenses = models.IntegerField(blank=True)
    country = CountryField(blank_label='(select country)')
    status = models.CharField(max_length=10,choices=STATUS)
    estimated_closing_date = models.DateField(blank=True)
    services = models.CharField(max_length=20,choices=CHOICES)


    def __str__(self):
        return f'{self.company}'


class CallReport(models.Model):
    CHOICES = (
                ('Video Conference','Video Conference'),
                ('Phone Call','Phone Call'),
                ('Lunch Meeting', 'Lunch Meeting'),
                ('Client Offices', "Client Offices")
               )
    company = models.ForeignKey(Company, on_delete=models.CASCADE, default="company")
    minutes_of_meeting = models.TextField(blank=True, null=True)
    client_POC = models.ForeignKey(Client, on_delete=models.CASCADE, default='client')

    date = models.DateField(blank=True)
    ACTeQ_representative = models.ForeignKey(Profile, on_delete=models.CASCADE, default='agent')


    def __str__(self):
        return f'Call Report for {self.company}, on the {self.date}'

class Deal(models.Model):
    project_id = models.ForeignKey(Leads, on_delete=models.CASCADE, default='id')
    agent = models.ForeignKey(Profile, on_delete=models.CASCADE, default="agent")
    service = models.ForeignKey(Leads, on_delete=models.CASCADE, related_name='service')
    closing_date = models.DateField(auto_now_add=True)
    client = models.ForeignKey(Client, on_delete=models.CASCADE,default='client')
    licenses = models.ForeignKey(Leads,on_delete=models.CASCADE, related_name='licenses')
    revenue = MoneyField(max_digits=14, decimal_places=2, default_currency='USD')
    comments = models.TextField(blank=True,null=True)


Traceback

Traceback (most recent call last):
 tial...Traceback (most recent call last):
  File "C:UsersfcolinaAppDataLocalPackagesPythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0LocalCachelocal-packagesPython39site-packagesdjangodbmodelsfields__init__.py", line 1823, in get_prep_value
    return int(value)
ValueError: invalid literal for int() with base 10: 'company'


ValueError: Field 'id' expected a number but got 'company'.

Any ideas?, please refrain yourself from suggesting deleting the db, migrations files, yes of course that solves some issues in particular when a table is added, then obviously the DB has not knowledge of that, but in this case, that trick won’t fix the issue

Asked By: Francisco Colina

||

Answers:

The option default='company' in Client, Leads and CallReport must be the error, make sure after remove it run the following commands:

python manange.py makemigrations

And after that run the following:

python manage.py migrate
Answered By: allexiusw

"A ForeignKey contains the primary key of the target table. If the primary key of that table relating to the company value is an integer, then your default value must be that integer", not that string

then, Remember to delete the database, and the migrations inside the migrations folder, and Then

python manange.py makemigrations

Run the following:

python manage.py migrate

This should resolve your issue.

Answered By: barna
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.