﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
33858	Altering model field from bigint to jsonb fails on PostgreSQL.	Ben Douglas	nobody	"I created a field for a model that I initially wanted to be a foreign key, but then decided I wanted the field to be a JSON object to store just a bit of information instead. But when changing the field type and then trying to migrate the system gave me an error - it seems that Postgres does not like casting values from one to the other, understandably. The first time it happened I went down a dark rabbit hole that ended up in me completely remaking the entire database, which was painful, and the second time I just renamed the field, deleted the original, made some modifications to the migration file, and changed the name back - which was also a pain but not as bad and a viable workaround.

Here is the terminal output when running the migrate (the field being changed is 'dependent_on_question' on the 'intake' app/model):

{{{
python manage.py migrate       

Operations to perform:
  Apply all migrations: [blah blah blah]
Running migrations:
  Applying intake.0002_alter_intakequestion_options_and_more...Traceback (most recent call last):
  File ""/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py"", line 89, in _execute
    return self.cursor.execute(sql, params)
psycopg2.errors.CannotCoerce: cannot cast type bigint to jsonb
LINE 1: ...on_question"" TYPE jsonb USING ""dependent_on_question""::jsonb
                                                                ^


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File ""/Users/[my user]/Sites/[my project]/api/manage.py"", line 22, in <module>
    main()
  File ""/Users/[my user]/Sites/[my project]/api/manage.py"", line 18, in main
    execute_from_command_line(sys.argv)
  File ""/usr/local/lib/python3.9/site-packages/django/core/management/__init__.py"", line 446, in execute_from_command_line
    utility.execute()
  File ""/usr/local/lib/python3.9/site-packages/django/core/management/__init__.py"", line 440, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File ""/usr/local/lib/python3.9/site-packages/django/core/management/base.py"", line 414, in run_from_argv
    self.execute(*args, **cmd_options)
  File ""/usr/local/lib/python3.9/site-packages/django/core/management/base.py"", line 460, in execute
    output = self.handle(*args, **options)
  File ""/usr/local/lib/python3.9/site-packages/django/core/management/base.py"", line 98, in wrapped
    res = handle_func(*args, **kwargs)
  File ""/usr/local/lib/python3.9/site-packages/django/core/management/commands/migrate.py"", line 290, in handle
    post_migrate_state = executor.migrate(
  File ""/usr/local/lib/python3.9/site-packages/django/db/migrations/executor.py"", line 131, in migrate
    state = self._migrate_all_forwards(
  File ""/usr/local/lib/python3.9/site-packages/django/db/migrations/executor.py"", line 163, in _migrate_all_forwards
    state = self.apply_migration(
  File ""/usr/local/lib/python3.9/site-packages/django/db/migrations/executor.py"", line 248, in apply_migration
    state = migration.apply(state, schema_editor)
  File ""/usr/local/lib/python3.9/site-packages/django/db/migrations/migration.py"", line 131, in apply
    operation.database_forwards(
  File ""/usr/local/lib/python3.9/site-packages/django/db/migrations/operations/fields.py"", line 235, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File ""/usr/local/lib/python3.9/site-packages/django/db/backends/base/schema.py"", line 747, in alter_field
    self._alter_field(
  File ""/usr/local/lib/python3.9/site-packages/django/db/backends/postgresql/schema.py"", line 231, in _alter_field
    super()._alter_field(
  File ""/usr/local/lib/python3.9/site-packages/django/db/backends/base/schema.py"", line 963, in _alter_field
    self.execute(
  File ""/usr/local/lib/python3.9/site-packages/django/db/backends/base/schema.py"", line 192, in execute
    cursor.execute(sql, params)
  File ""/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py"", line 103, in execute
    return super().execute(sql, params)
  File ""/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py"", line 67, in execute
    return self._execute_with_wrappers(
  File ""/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py"", line 80, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File ""/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py"", line 89, in _execute
    return self.cursor.execute(sql, params)
  File ""/usr/local/lib/python3.9/site-packages/django/db/utils.py"", line 91, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File ""/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py"", line 89, in _execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: cannot cast type bigint to jsonb
LINE 1: ...on_question"" TYPE jsonb USING ""dependent_on_question""::jsonb
}}}
"	Bug	closed	Migrations	3.2	Normal	invalid	migration, json, jsonb, postgres		Unreviewed	0	0	0	0	0	0
