You are trying to add a non-nullable field 'name' to contact without a default error handling in Django database operations

name = models.CharField(max_length=50)
执行:python manage.py makemirations出现以下错误:


You are trying to add a non-nullable field 'name' to contact 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: 

Reason: Because a new field is added, and the previous data must have no value, but this field cannot be empty, so a default value is required. Alternatively, this field can be allowed to be empty.


Solution:
first give any initial value of 'name': name = models.CharField(max_length=50, default='')
and then execute: python manage.py makemirations
and then execute: python manage.py migrate and


then delete default, that is Execute: name = models.CharField(max_length=50)
Execute: python manage.py makemirations
and then execute: python manage.py migrate


to solve!



Note: During the development process, after the database synchronization misoperation, it is inevitable to encounter the situation that the synchronization cannot be successful later. A simple and rude way to solve this problem is to delete all the scripts in the migrations directory (except __init__.py) Delete the database, create a new database after deleting the database, and do the database synchronization operation again. 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325207046&siteId=291194637