models will not let me migrate nor delete a field

This has been driving me out of my mind for the past couple of hours.I am defining a database in models and added a TextField. When I transfer to Python to add values in bulk, it objects to “blank=True” and then objects because the field has no default value.
So I go back to django and add a series of different default values, but when I migrate, the error-checker tells me Blobs, Text or JSON columns cannot have a default value.
Subsequently migrate flags this error even after I have removed the field entirely, restarted VScode, and even restarted the computer. I do not want to have to start over reconstructing the program from scratch.


class Images(models.Model):
    accession_code = models.CharField(max_length=50, unique=True, db_index=True)
    date_year = models.CharField(max_length=4, db_default="0000")
    date_month = models.CharField(max_length=4, db_default="00")
    date_day = models.CharField(max_length=4, db_default="00")
    caption = models.CharField(max_length=50, db_default="Enter caption", blank=True)
    raw_file_exists = models.BooleanField(db_default=False)
    envelope_id = models.CharField(
        max_length=50,
        blank=True,
        help_text="To link to Lisa's physical prints some day"
    )
    """
    image_comment = models.CharField(max_length=255, db_default="Further comments")
    """
    photographers_id = models.IntegerField
    orientation_vertical = models.BooleanField(db_default=False)
    access_level = models.OneToOneField(
        Access_Levels, 
        on_delete=models.RESTRICT,
    )
    # the django interpreter would not let me have a textfield for comments with or without a default

    def __str__(self):
        return self.accession_code 

For us to try and help you with this, you’re going to need to post the code that is not working, along with the complete traceback of whatever errors you are getting. Providing only a description that something isn’t working or is being rejected is not enough information for us to work from.

This includes posting whatever warnings or errors you are getting with makemigrations or migrate along with the model that is causing the error.

I’m new at this; so if there is a better way to send this info,please tell me. Thank you for your willingness to help. I’m sending this all to a mySQL database.

(env) PS C:\dbDeerfield240915\dfArchive> py manage.py migrate       
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, posts, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying auth.0012_alter_user_first_name_max_length... OK
  Applying posts.0001_initial... OK
  Applying posts.0002_access_levels_remove_post_body_remove_post_date_and_more... OK
  Applying posts.0003_images... OK
  Applying posts.0004_keywords... OK
  Applying posts.0005_photographers... OK
  Applying posts.0006_alter_images_date_day_alter_images_date_month_and_more... OK
  Applying posts.0007_alter_images_date_day_alter_images_date_month_and_more... OK
  Applying posts.0008_alter_images_caption_alter_images_envelope_id... OK
  Applying posts.0009_alter_images_caption... OK
  Applying posts.0010_alter_images_image_comments...Traceback (most recent call last):
  File "C:\dbDeerfield240915\env\Lib\site-packages\django\db\backends\utils.py", line 105, in _execute
    return self.cursor.execute(sql, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\dbDeerfield240915\env\Lib\site-packages\django\db\backends\mysql\base.py", line 76, in execute        
    return self.cursor.execute(query, args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\dbDeerfield240915\env\Lib\site-packages\MySQLdb\cursors.py", line 179, in execute
    res = self._query(mogrified_query)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\dbDeerfield240915\env\Lib\site-packages\MySQLdb\cursors.py", line 330, in _query
    db.query(q)
  File "C:\dbDeerfield240915\env\Lib\site-packages\MySQLdb\connections.py", line 261, in query
    _mysql.connection.query(self, query)
MySQLdb.OperationalError: (1101, "BLOB, TEXT, GEOMETRY or JSON column 'image_comments' can't have a default value")

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\dbDeerfield240915\dfArchive\manage.py", line 22, in <module>
    main()
  File "C:\dbDeerfield240915\dfArchive\manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:\dbDeerfield240915\env\Lib\site-packages\django\core\management\__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "C:\dbDeerfield240915\env\Lib\site-packages\django\core\management\__init__.py", line 436, in execute     
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\dbDeerfield240915\env\Lib\site-packages\django\core\management\base.py", line 413, in run_from_argv   
    self.execute(*args, **cmd_options)
  File "C:\dbDeerfield240915\env\Lib\site-packages\django\core\management\base.py", line 459, in execute
    output = self.handle(*args, **options)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\dbDeerfield240915\env\Lib\site-packages\django\core\management\base.py", line 107, in wrapper
    res = handle_func(*args, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\dbDeerfield240915\env\Lib\site-packages\django\core\management\commands\migrate.py", line 357, in handle
    post_migrate_state = executor.migrate(
                         ^^^^^^^^^^^^^^^^^
  File "C:\dbDeerfield240915\env\Lib\site-packages\django\db\migrations\executor.py", line 135, in migrate       
    state = self._migrate_all_forwards(
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\dbDeerfield240915\env\Lib\site-packages\django\db\migrations\executor.py", line 167, in _migrate_all_forwards
    state = self.apply_migration(
            ^^^^^^^^^^^^^^^^^^^^^
  File "C:\dbDeerfield240915\env\Lib\site-packages\django\db\migrations\executor.py", line 255, in apply_migration
    state = migration.apply(state, schema_editor)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\dbDeerfield240915\env\Lib\site-packages\django\db\migrations\migration.py", line 132, in apply        
    operation.database_forwards(
  File "C:\dbDeerfield240915\env\Lib\site-packages\django\db\migrations\operations\fields.py", line 241, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "C:\dbDeerfield240915\env\Lib\site-packages\django\db\backends\base\schema.py", line 905, in alter_field  
  File "C:\dbDeerfield240915\env\Lib\site-packages\django\db\backends\base\schema.py", line 1162, in _alter_field    self.execute(
  File "C:\dbDeerfield240915\env\Lib\site-packages\django\db\backends\base\schema.py", line 202, in execute      
  File "C:\dbDeerfield240915\env\Lib\site-packages\django\db\backends\utils.py", line 122, in execute
    return super().execute(sql, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\dbDeerfield240915\env\Lib\site-packages\django\db\backends\utils.py", line 79, in execute
    return self._execute_with_wrappers(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\dbDeerfield240915\env\Lib\site-packages\django\db\backends\utils.py", line 92, in _execute_with_wrappers
    return executor(sql, params, many, context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\dbDeerfield240915\env\Lib\site-packages\django\db\backends\utils.py", line 100, in _execute
    with self.db.wrap_database_errors:
  File "C:\dbDeerfield240915\env\Lib\site-packages\django\db\utils.py", line 91, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "C:\dbDeerfield240915\env\Lib\site-packages\django\db\backends\utils.py", line 105, in _execute
    return self.cursor.execute(sql, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\dbDeerfield240915\env\Lib\site-packages\django\db\backends\mysql\base.py", line 76, in execute        
    return self.cursor.execute(query, args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\dbDeerfield240915\env\Lib\site-packages\MySQLdb\cursors.py", line 179, in execute
    res = self._query(mogrified_query)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\dbDeerfield240915\env\Lib\site-packages\MySQLdb\cursors.py", line 330, in _query
    db.query(q)
  File "C:\dbDeerfield240915\env\Lib\site-packages\MySQLdb\connections.py", line 261, in query
    _mysql.connection.query(self, query)
django.db.utils.OperationalError: (1101, "BLOB, TEXT, GEOMETRY or JSON column 'image_comments' can't have a default value")
(env) PS C:\dbDeerfield240915\dfArchive> py manage.py makemigrations
Migrations for 'posts':
  posts\migrations\0022_remove_images_image_comment.py
    - Remove field image_comment from images
(env) PS C:\dbDeerfield240915\dfArchive> py manage.py migrate       
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, posts, sessions
Running migrations:
  Applying posts.0010_alter_images_image_comments...Traceback (most recent call last):
  File "C:\dbDeerfield240915\env\Lib\site-packages\django\db\backends\utils.py", line 105, in _execute
    return self.cursor.execute(sql, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\dbDeerfield240915\env\Lib\site-packages\django\db\backends\mysql\base.py", line 76, in execute        
    return self.cursor.execute(query, args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\dbDeerfield240915\env\Lib\site-packages\MySQLdb\cursors.py", line 179, in execute
    res = self._query(mogrified_query)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\dbDeerfield240915\env\Lib\site-packages\MySQLdb\cursors.py", line 330, in _query
    db.query(q)
  File "C:\dbDeerfield240915\env\Lib\site-packages\MySQLdb\connections.py", line 261, in query
    _mysql.connection.query(self, query)
MySQLdb.OperationalError: (1101, "BLOB, TEXT, GEOMETRY or JSON column 'image_comments' can't have a default value")

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\dbDeerfield240915\dfArchive\manage.py", line 22, in <module>
    main()
  File "C:\dbDeerfield240915\dfArchive\manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:\dbDeerfield240915\env\Lib\site-packages\django\core\management\__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "C:\dbDeerfield240915\env\Lib\site-packages\django\core\management\__init__.py", line 436, in execute     
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\dbDeerfield240915\env\Lib\site-packages\django\core\management\base.py", line 413, in run_from_argv   
    self.execute(*args, **cmd_options)
  File "C:\dbDeerfield240915\env\Lib\site-packages\django\core\management\base.py", line 459, in execute
    output = self.handle(*args, **options)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\dbDeerfield240915\env\Lib\site-packages\django\core\management\base.py", line 107, in wrapper
    res = handle_func(*args, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\dbDeerfield240915\env\Lib\site-packages\django\core\management\commands\migrate.py", line 357, in handle
    post_migrate_state = executor.migrate(
                         ^^^^^^^^^^^^^^^^^
  File "C:\dbDeerfield240915\env\Lib\site-packages\django\db\migrations\executor.py", line 135, in migrate       
    state = self._migrate_all_forwards(
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\dbDeerfield240915\env\Lib\site-packages\django\db\migrations\executor.py", line 167, in _migrate_all_forwards
    state = self.apply_migration(
            ^^^^^^^^^^^^^^^^^^^^^
  File "C:\dbDeerfield240915\env\Lib\site-packages\django\db\migrations\executor.py", line 255, in apply_migration
    state = migration.apply(state, schema_editor)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\dbDeerfield240915\env\Lib\site-packages\django\db\migrations\migration.py", line 132, in apply        
    operation.database_forwards(
  File "C:\dbDeerfield240915\env\Lib\site-packages\django\db\migrations\operations\fields.py", line 241, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "C:\dbDeerfield240915\env\Lib\site-packages\django\db\backends\base\schema.py", line 905, in alter_field  
    self._alter_field(
  File "C:\dbDeerfield240915\env\Lib\site-packages\django\db\backends\base\schema.py", line 1162, in _alter_field    self.execute(
  File "C:\dbDeerfield240915\env\Lib\site-packages\django\db\backends\base\schema.py", line 202, in execute      
    cursor.execute(sql, params)
  File "C:\dbDeerfield240915\env\Lib\site-packages\django\db\backends\utils.py", line 122, in execute
    return super().execute(sql, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\dbDeerfield240915\env\Lib\site-packages\django\db\backends\utils.py", line 79, in execute
    return self._execute_with_wrappers(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\dbDeerfield240915\env\Lib\site-packages\django\db\backends\utils.py", line 92, in _execute_with_wrappers
    return executor(sql, params, many, context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\dbDeerfield240915\env\Lib\site-packages\django\db\backends\utils.py", line 100, in _execute
    with self.db.wrap_database_errors:
  File "C:\dbDeerfield240915\env\Lib\site-packages\django\db\utils.py", line 91, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "C:\dbDeerfield240915\env\Lib\site-packages\django\db\backends\utils.py", line 105, in _execute
    return self.cursor.execute(sql, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\dbDeerfield240915\env\Lib\site-packages\django\db\backends\mysql\base.py", line 76, in execute        
    return self.cursor.execute(query, args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\dbDeerfield240915\env\Lib\site-packages\MySQLdb\cursors.py", line 179, in execute
    res = self._query(mogrified_query)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\dbDeerfield240915\env\Lib\site-packages\MySQLdb\cursors.py", line 330, in _query
    db.query(q)
  File "C:\dbDeerfield240915\env\Lib\site-packages\MySQLdb\connections.py", line 261, in query
    _mysql.connection.query(self, query)
django.db.utils.OperationalError: (1101, "BLOB, TEXT, GEOMETRY or JSON column 'image_comments' can't have a default value")
(env) PS C:\dbDeerfield240915\dfArchive> 

models.py…

from django.db import models

# Create your models here.

class Access_Levels(models.Model):
    access_level = models.CharField(max_length=15, help_text='Management, Staff, Public')
    
    def __str__(self):
        return self.access_level

class Images(models.Model):
    accession_code = models.CharField(max_length=50, unique=True, db_index=True)
    date_year = models.CharField(max_length=4, db_default="0000")
    date_month = models.CharField(max_length=4, db_default="00")
    date_day = models.CharField(max_length=4, db_default="00")
    caption = models.CharField(max_length=50, db_default="Enter caption", blank=True)
    raw_file_exists = models.BooleanField(db_default=False)
    envelope_id = models.CharField(
        max_length=50,
        blank=True,
        help_text="To link to Lisa's physical prints some day"
    )  
    photographers_id = models.IntegerField
    orientation_vertical = models.BooleanField(db_default=False)
    access_level = models.OneToOneField(
        Access_Levels, 
        on_delete=models.RESTRICT,
    )
    # the django interpreter would not let me have a textfield for comments with or without a default

    def __str__(self):
        return self.accession_code 

        
class Photographers(models.Model):
    fname = models.CharField(max_length=50)
    lname = models.CharField(max_length=50)
    email = models.EmailField
    address_street = models.CharField(max_length=50)
    address_city = models.CharField(max_length=50)
    address_state = models.CharField(max_length=50)
    address_zip = models.CharField(max_length=50)
    last_updated = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        full_name = self.lname + ", " + self.fname
        return self.full_name  


class Keywords(models.Model):
    keyword = models.CharField(max_length=50)
    Images_image_id = models.ManyToManyField(Images)

    def __str__(self):
        return self.keyword

class Post(models.Model):
    title = models.CharField (max_length=50)

The first step will be to get things back to a more stable state.

Please show the output of a py manage.py showmigrations command.

You’re probably going to want to delete the migrations from the posts app starting with 0010 and higher, but we need to verify that there aren’t other unapplied migrations affected by this.

I guess the result confirms that all migrations have been cleared.
I have attached the migration folder in case that is helpful.

–Adrian

(Attachment migrations.zip is missing)

(attachments)

Sorry, I can’t read what you posted here. Please copy/paste the output of a showmigrations command into the body of your post as preformatted text, just like you did with your code.

(env) PS C:\dbDeerfield240915\dfArchive> py manage,py showmigrations
 *  History restored 

C:\Python312\python.exe: can't open file 'C:\\dbDeerfield240915\\dfArchive\\manage,py': [Errno 2] No such file or directory

Read the full error message. What is it telling you?

sorry to have missed the comma :grimacing:

(env) PS C:\dbDeerfield240915\dfArchive> py manage.py showmigrations
admin
 [X] 0001_initial
 [X] 0002_logentry_remove_auto_add
 [X] 0003_logentry_add_action_flag_choices
auth
 [X] 0001_initial
 [X] 0002_alter_permission_name_max_length
 [X] 0003_alter_user_email_max_length
 [X] 0004_alter_user_username_opts
 [X] 0005_alter_user_last_login_null
 [X] 0006_require_contenttypes_0002
 [X] 0007_alter_validators_add_error_messages
 [X] 0008_alter_user_username_max_length
 [X] 0009_alter_user_last_name_max_length
 [X] 0010_alter_group_name_max_length
 [X] 0011_update_proxy_permissions
 [X] 0012_alter_user_first_name_max_length
contenttypes
 [X] 0001_initial
 [X] 0002_remove_content_type_name
posts
 [X] 0001_initial
 [X] 0002_access_levels_remove_post_body_remove_post_date_and_more
 [X] 0003_images
 [X] 0004_keywords
 [X] 0005_photographers
 [X] 0006_alter_images_date_day_alter_images_date_month_and_more
 [X] 0007_alter_images_date_day_alter_images_date_month_and_more
 [X] 0008_alter_images_caption_alter_images_envelope_id
 [X] 0009_alter_images_caption
 [ ] 0010_alter_images_image_comments
 [ ] 0011_alter_images_image_comments
 [ ] 0012_alter_images_image_comments
 [ ] 0013_alter_images_image_comments
 [ ] 0014_remove_images_image_comments
 [ ] 0015_images_image_comments
 [ ] 0016_alter_images_image_comments
 [ ] 0017_alter_images_image_comments
 [ ] 0018_remove_images_image_comments
 [ ] 0019_images_image_comment
 [ ] 0020_remove_images_image_comment
 [ ] 0021_images_image_comment
 [ ] 0022_remove_images_image_comment
sessions
 [ ] 0001_initial

Ok, since none of the migrations from 0010 onward in your posts app have been applied, I’d delete them, run migrate, and verify you’re at a currently-stable environment.
Then make the desired model change, rerun makemigrations, and see if it works.

2 Likes

That worked. Thank you very much for your help and your patience.

Mark the post that helped you as a solution in order to help others :slight_smile: