Django Deploy Problem

I followed through all the course and everything worked fine. In the last part of the course (Deployment), however, i struggled to deploy with MySQL, so i changed to the Heroku Postgres add-on.

Below, you can see the problem I had after commiting to git and doing git push heroku master. Anybody knows the solution to the following?

[…]
remote: Verifying deploy… done.
remote: Running release command…
remote:
remote: Operations to perform:
remote: Apply all migrations: admin, auth, contenttypes, core, likes, sessions, store, tags
remote: Running migrations:
remote: Applying store.0007_alter_cart_id…Traceback (most recent call last):
remote: File “/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/utils.py”, line 89, in _execute
remote: return self.cursor.execute(sql, params)
remote: psycopg2.errors.CannotCoerce: cannot cast type bigint to uuid
remote: LINE 1: …LE “store_cart” ALTER COLUMN “id” TYPE uuid USING “id”::uuid
remote: ^
remote:
remote:
remote: The above exception was the direct cause of the following exception:
remote:
remote: Traceback (most recent call last):
remote: File “/app/manage.py”, line 22, in
remote: main()
remote: File “/app/manage.py”, line 18, in main
remote: execute_from_command_line(sys.argv)
remote: File “/app/.heroku/python/lib/python3.9/site-packages/django/core/management/init.py”, line 446, in execute_from_command_line
remote: utility.execute()
remote: File “/app/.heroku/python/lib/python3.9/site-packages/django/core/management/init.py”, line 440, in execute
remote: self.fetch_command(subcommand).run_from_argv(self.argv)
remote: File “/app/.heroku/python/lib/python3.9/site-packages/django/core/management/base.py”, line 402, in run_from_argv
remote: self.execute(*args, **cmd_options)
remote: File “/app/.heroku/python/lib/python3.9/site-packages/django/core/management/base.py”, line 448, in execute
remote: output = self.handle(*args, **options)
remote: File “/app/.heroku/python/lib/python3.9/site-packages/django/core/management/base.py”, line 96, in wrapped
remote: res = handle_func(*args, **kwargs)
remote: File “/app/.heroku/python/lib/python3.9/site-packages/django/core/management/commands/migrate.py”, line 349, in handle
remote: post_migrate_state = executor.migrate(
remote: File “/app/.heroku/python/lib/python3.9/site-packages/django/db/migrations/executor.py”, line 135, in migrate
remote: state = self._migrate_all_forwards(
remote: File “/app/.heroku/python/lib/python3.9/site-packages/django/db/migrations/executor.py”, line 167, in _migrate_all_forwards
remote: state = self.apply_migration(
remote: File “/app/.heroku/python/lib/python3.9/site-packages/django/db/migrations/executor.py”, line 252, in apply_migration
remote: state = migration.apply(state, schema_editor)
remote: File “/app/.heroku/python/lib/python3.9/site-packages/django/db/migrations/migration.py”, line 130, in apply
remote: operation.database_forwards(
remote: File “/app/.heroku/python/lib/python3.9/site-packages/django/db/migrations/operations/fields.py”, line 235, in database_forwards
remote: schema_editor.alter_field(from_model, from_field, to_field)
remote: File “/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/base/schema.py”, line 788, in alter_field
remote: self._alter_field(
remote: File “/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/postgresql/schema.py”, line 246, in _alter_field
remote: super()._alter_field(
remote: File “/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/base/schema.py”, line 1007, in _alter_field
remote: self.execute(
remote: File “/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/base/schema.py”, line 199, in execute
remote: cursor.execute(sql, params)
remote: File “/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/utils.py”, line 67, in execute
remote: return self._execute_with_wrappers(
remote: File “/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/utils.py”, line 80, in _execute_with_wrappers
remote: return executor(sql, params, many, context)
remote: File “/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/utils.py”, line 89, in _execute
remote: return self.cursor.execute(sql, params)
remote: File “/app/.heroku/python/lib/python3.9/site-packages/django/db/utils.py”, line 91, in exit
remote: raise dj_exc_value.with_traceback(traceback) from exc_value
remote: File “/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/utils.py”, line 89, in _execute
remote: return self.cursor.execute(sql, params)
remote: django.db.utils.ProgrammingError: cannot cast type bigint to uuid
remote: LINE 1: …LE “store_cart” ALTER COLUMN “id” TYPE uuid USING “id”::uuid
remote: ^
remote:
remote: Waiting for release… failed.

I’m having the same problem deploying on a local Postgres db.
Found this article: django - Cannot cast type integer to uuid - Stack Overflow

Haven’t been able to figure out how to get it to work after attempting what feels like a 100 variations of migrations.

Have you had any luck?

EDIT: FIGURED IT OUT!! :grinning:

We’re attempting to alter a database column to use the uuid data type, but the current values in the column cannot be cast to the uuid type. In our case the store_cart (id) column is set to bigint, which can’t be cast to a uuid.

I attempted to delete the store_cart (id) field and recreate with uuid format. This didn’t work because the store_cartitem (cart_id) references this field. So we’ll have to remove the dependencies and rebuild.

Steps are:

  • delete store_cartitem(cart_id) field
  • Now with no dependency the store_cart(id) field can be manually changed to uuid format
  • create the store_cartitem(cart_id) field and add the FOREIGN KEY reference to store_cart(id)
  • Run migration

DONE!

** Even after learning to code for the past 6 months and getting to Part 3 of the Django series this is probably the first time I’ve felt (mildly) competent at coding haha. #progress

1 Like

Even I am also getting same error. After adding UUID in cart model, It is showing the
(django.db.utils.ProgrammingError: cannot cast type bigint to uuid
LINE 1: …LE “store_cart” ALTER COLUMN “id” TYPE uuid USING “id”::uuid) error. I deleted migration file and again migrate it but still I am facing this issue. I am using sqlite3 database so should I use postgresql database, please give me hint for it.

Thank you!!

When you use postgres (like me), to solve this problem follow the path below:

  1. First, you must go to the model store and comment on the Cart and the CartItem.
  2. Create a migration (removing these two models) and then migrate.
  3. Then remove the two models from the comment and change the ID to uuid.
  4. And at the end create a migration and then migrate.

I found this method to solve this problem, I hope it will be useful for you.