0

I have an Employee model in my django app like the following:

class Employee(models.Model):
    role_choices = (('CRUD', 'CRUD'), ('View', 'View'))
    user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True)
    company = models.ForeignKey(Company, on_delete=models.CASCADE, related_name="company")
    name = models.CharField(max_length=500, default=0)
    email = models.EmailField(max_length=500, default=0)
    phone = models.CharField(max_length=500, default=0)
    city = models.CharField(max_length=500, default=0)

But now I want to remove this user field but since it's primaryKey it is asking me to do this:

You are trying to add a non-nullable field 'id' to employee without a default; we can't do that (the database needs something to populate existing rows).
Please select a fix:
 1) Provide a one-off default now (will be set on all existing rows with a null value for this column)
 2) Quit, and let me add a default in models.py
Select an option: 

providing a value will result in an error since there is no point in giving a single id as default value. How to solve this ?

Rahul Sharma
  • 2,187
  • 6
  • 31
  • 76

2 Answers2

0

Remove all the data which are in the user field.

2nd option

python manage.py makemigrations app_name
python manage.py migrate --run-syncdb
LeoE
  • 2,054
  • 1
  • 11
  • 29
-2

You need to set another PK for your table to be able to delete the previous one.

For example:

    name = models.CharField(max_length=500, primary_key=True, default=0)