Django: cannot cast type integer to time without time zone

I've had some previous testing with models and their functionality integration. There were few fields that were using for time input. At first I thought maybe IntegerField will be enough, but as we continue with more new solutions things changed. So there was check_in_time, check_out_time and etc.

Now we want to set it to TimeField, but as we are trying to apply the migrations those errors appear. I'm not an postgres guru so I'm scared of breaking things up, cuz migrations might be an pain in the ass.

This is the new migration:

class Migration(migrations.Migration):

    dependencies = [
        ('property', '0084_rename_max_tenants_property_max_guests_allowed'),
    ]

    operations = [
        migrations.AlterField(
            model_name='property',
            name='check_in_after_time',
            field=models.TimeField(null=True),
        ),
        migrations.AlterField(
            model_name='property',
            name='check_in_before_time',
            field=models.TimeField(null=True),
        ),
        migrations.AlterField(
            model_name='property',
            name='check_out_after_time',
            field=models.TimeField(null=True),
        ),
        migrations.AlterField(
            model_name='property',
            name='check_out_before_time',
            field=models.TimeField(null=True),
        ),
    ]

This is our fields that we want to use it right now as TimeFields:

    check_in_before_time = models.TimeField(null=True)
    check_in_after_time = models.TimeField(null=True)
    notice_days_before_check_in = models.PositiveIntegerField(default=0)

    check_out_before_time = models.TimeField(null=True)
    check_out_after_time = models.TimeField(null=True)

How can I fix it now and prevent it from happening again when migrating this on other servers?

LOG:

bin/django migrate property
Operations to perform:
  Apply all migrations: property
Running migrations:
  Applying property.0085_auto_20230122_0304...Traceback (most recent call last):
  File "/home/dziugas/werent/app/venv/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
psycopg2.errors.CannotCoerce: cannot cast type integer to time without time zone
LINE 1: ...k_in_after_time" TYPE time USING "check_in_after_time"::time
                                                                 ^


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

Traceback (most recent call last):
  File "/home/dziugas/werent/app/bin/django", line 11, in <module>
    load_entry_point('web', 'console_scripts', 'django')()
  File "/home/dziugas/werent/app/web/manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/home/dziugas/werent/app/venv/lib/python3.9/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "/home/dziugas/werent/app/venv/lib/python3.9/site-packages/django/core/management/__init__.py", line 413, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/dziugas/werent/app/venv/lib/python3.9/site-packages/django/core/management/base.py", line 354, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/dziugas/werent/app/venv/lib/python3.9/site-packages/django/core/management/base.py", line 398, in execute
    output = self.handle(*args, **options)
  File "/home/dziugas/werent/app/venv/lib/python3.9/site-packages/django/core/management/base.py", line 89, in wrapped
    res = handle_func(*args, **kwargs)
  File "/home/dziugas/werent/app/venv/lib/python3.9/site-packages/django/core/management/commands/migrate.py", line 244, in handle
    post_migrate_state = executor.migrate(
  File "/home/dziugas/werent/app/venv/lib/python3.9/site-packages/django/db/migrations/executor.py", line 117, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/home/dziugas/werent/app/venv/lib/python3.9/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/home/dziugas/werent/app/venv/lib/python3.9/site-packages/django/db/migrations/executor.py", line 227, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/home/dziugas/werent/app/venv/lib/python3.9/site-packages/django/db/migrations/migration.py", line 126, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/home/dziugas/werent/app/venv/lib/python3.9/site-packages/django/db/migrations/operations/fields.py", line 244, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "/home/dziugas/werent/app/venv/lib/python3.9/site-packages/django/db/backends/base/schema.py", line 608, in alter_field
    self._alter_field(model, old_field, new_field, old_type, new_type,
  File "/home/dziugas/werent/app/venv/lib/python3.9/site-packages/django/db/backends/postgresql/schema.py", line 196, in _alter_field
    super()._alter_field(
  File "/home/dziugas/werent/app/venv/lib/python3.9/site-packages/django/db/backends/base/schema.py", line 765, in _alter_field
    self.execute(
  File "/home/dziugas/werent/app/venv/lib/python3.9/site-packages/django/db/backends/base/schema.py", line 145, in execute
    cursor.execute(sql, params)
  File "/home/dziugas/werent/app/venv/lib/python3.9/site-packages/django/db/backends/utils.py", line 98, in execute
    return super().execute(sql, params)
  File "/home/dziugas/werent/app/venv/lib/python3.9/site-packages/django/db/backends/utils.py", line 66, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/home/dziugas/werent/app/venv/lib/python3.9/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/home/dziugas/werent/app/venv/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/home/dziugas/werent/app/venv/lib/python3.9/site-packages/django/db/utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/home/dziugas/werent/app/venv/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: cannot cast type integer to time without time zone
LINE 1: ...k_in_after_time" TYPE time USING "check_in_after_time"::time
Back to Top