Click here to Skip to main content
15,880,608 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Python
import uuid
from django.db import models
from django.core import validators

class Author(models.Model):

    class Meta:
        verbose_name = 'Автор'
        verbose_name_plural = 'Авторы'
        ordering = ['id']
        unique_together = ('name', 'age')

    TYPES = (
        ('a', 'forein'),
        ('b', 'domestic'),
        ('c', 'other')
    )
    id = models.UUIDField(primary_key=True, db_index=True, default=uuid.uuid4)
    name = models.CharField(verbose_name='Имя автора', 
    max_length=200,
     validators=[validators.RegexValidator(regex='^.*em$', message='Wrong')])
    age = models.PositiveIntegerField(verbose_name='Возраст автора')
    email = models.EmailField(verbose_name='Почта автора')
    lit_type = models.CharField(max_length=1, verbose_name='Тип литиратуры', choices=TYPES, default='a')
    
    def __str__(self):
        return self.name


class Book(models.Model):

    class Meta:
        verbose_name = 'Книга'
        verbose_name_plural = 'Книги'
        get_latest_by = 'published'

    title = models.CharField(max_length=200)
    description = models.TextField()
    page_num = models.PositiveIntegerField()
    published = models.DateTimeField(auto_now_add=True)
    author = models.ForeignKey(Author, on_delete=models.CASCADE)

    def __str__(self):
        return self.title

Terminal
<pre>(venv) PS C:\Users\user\Desktop\dz\new_pr> python manage.py makemigrations
It is impossible to add a non-nullable field 'author' to book without specifying a default. This is because 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 manually define a default value in models.py.
Select an option: 1
Please enter the default value as valid Python.
The datetime and django.utils.timezone modules are available, so it is possible to provide e.g. timezone.now as a value. 
Type 'exit' to exit this prompt
>>> Traceback (most recent call last):
  File "C:\Users\user\Desktop\dz\new_pr\manage.py", line 22, in <module>
    main()
  File "C:\Users\user\Desktop\dz\new_pr\manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\user\Desktop\dz\new_pr\venv\lib\site-packages\django\core\management\__init__.py", line 446, in execute_from_command_line
    utility.execute()
  File "C:\Users\user\Desktop\dz\new_pr\venv\lib\site-packages\django\core\management\__init__.py", line 440, in execute 
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\user\Desktop\dz\new_pr\venv\lib\site-packages\django\core\management\base.py", line 402, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\user\Desktop\dz\new_pr\venv\lib\site-packages\django\core\management\base.py", line 448, in execute     
    output = self.handle(*args, **options)
  File "C:\Users\user\Desktop\dz\new_pr\venv\lib\site-packages\django\core\management\base.py", line 96, in wrapped      
    res = handle_func(*args, **kwargs)
  File "C:\Users\user\Desktop\dz\new_pr\venv\lib\site-packages\django\core\management\commands\makemigrations.py", line 218, in handle
    changes = autodetector.changes(
  File "C:\Users\user\Desktop\dz\new_pr\venv\lib\site-packages\django\db\migrations\autodetector.py", line 46, in changes
    changes = self._detect_changes(convert_apps, graph)
  File "C:\Users\user\Desktop\dz\new_pr\venv\lib\site-packages\django\db\migrations\autodetector.py", line 195, in _detect_changes
    self.generate_added_fields()
  File "C:\Users\user\Desktop\dz\new_pr\venv\lib\site-packages\django\db\migrations\autodetector.py", line 1021, in generate_added_fields
    self._generate_added_field(app_label, model_name, field_name)
  File "C:\Users\user\Desktop\dz\new_pr\venv\lib\site-packages\django\db\migrations\autodetector.py", line 1054, in _generate_added_field
    field.default = self.questioner.ask_not_null_addition(
  File "C:\Users\user\Desktop\dz\new_pr\venv\lib\site-packages\django\db\migrations\questioner.py", line 186, in ask_not_null_addition
    return self._ask_default()
  File "C:\Users\user\Desktop\dz\new_pr\venv\lib\site-packages\django\db\migrations\questioner.py", line 151, in _ask_default
KeyboardInterrupt



What I have tried:

It doesn’t migrate for me, after I drive 1, it asks for an exit, I press o CTRL + C and that’s it, there is no migration,
Posted
Updated 22-Nov-22 21:58pm
Comments
Graeme_Grant 22-Nov-22 16:24pm    
The error message is very specific: "It is impossible to add a non-nullable field 'author' to book without specifying a default. This is because the database needs something to populate existing rows."
Eva Smirnova 23-Nov-22 0:18am    
So how should this be fixed?

When you press '1' it should be asking you for the default value to apply.
Ctrl-C will terminate your program without further processing taking place.

Quote:
So how should this be fixed?
You have several options:
1. Change the database so that the `author` column is nullable - PostgreSQL: Documentation: 7.3: ALTER TABLE[^]
2. Change the database so that a default value is provided when `author` is null - PostgreSQL: Documentation: 15: 5.2. Default Values[^]
3. Or, perhaps the best option, provide an actual value for `author` - you might want to look again at your model and determine why that value is not being returned
 
Share this answer
 
Add a default parameter to the name field in your Author class thus:
Python
name = models.CharField(verbose_name='Имя автора', default='no name',
max_length=200,
 validators=[validators.RegexValidator(regex='^.*em$', message='Wrong')])
 
Share this answer
 
Comments
Eva Smirnova 23-Nov-22 6:00am    
The problem was solved otherwise everything worked when I chose 1, then again chose 1 and everything worked.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900