Unable to migrate to database during build

Hello, I have a web app and a postgres db. When I deploy the web app without the python manage.py migrate command, it builds successfully. However, as I’m deploying a django app (API), I need to migrate so that it can access the database but when I add the migrate command I get the following error:

[INFO] Running custom build command: 'python manage.py migrate'
Python path configuration:
  PYTHONHOME = '/home/koyeb/.heroku/python'
  PYTHONPATH = '/home/koyeb'
  program name = 'python'
  isolated = 0
  environment = 1
  user site = 1
  import site = 1
  sys._base_executable = '/usr/bin/python'
  sys.base_prefix = '/home/koyeb/.heroku/python'
  sys.base_exec_prefix = '/home/koyeb/.heroku/python'
  sys.platlibdir = 'lib'
  sys.executable = '/usr/bin/python'
  sys.prefix = '/home/koyeb/.heroku/python'
  sys.exec_prefix = '/home/koyeb/.heroku/python'
  sys.path = [
    '/home/koyeb',
    '/home/koyeb/.heroku/python/lib/python310.zip',
    '/home/koyeb/.heroku/python/lib/python3.10',
    '/home/koyeb/.heroku/python/lib/python3.10/lib-dynload',
  ]
Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding
Python runtime state: core initialized
ModuleNotFoundError: No module named 'encodings'
Current thread 0x00007f431753a000 (most recent call first):
  <no Python frame>

I was using python3.12 but switched to python3.11 as the docs say python3.12 isn’t yet supported and got the same error. I’m new to Koyeb so I’m not sure how to solve this and whether it’s due to something I’ve done or something else.

Thank you :slight_smile:

Hello,
A quick update. I was able to run the migrate command in the console and all is working fine. I also reverted back to using python3.12 and again, no issues. However, I would still be interested to know if I can do the migrate during the build as having to do it manually in the console is a bit of a pain.
Thank you :slight_smile:

Hello @Catriona_Bennett,

I would not recommend running migration during build time as this may break your current deployment.
You should run migration before your app starts. You may create some shell script which will first run migration and the start the app.

Regards

Hi @Lukasz_Oles
Thank you for your reply. That’s interesting but given the issues I experienced when running migration during the build time, I can understand why I shouldn’t do it then. However, it’s good to know that I can do it before starting the app and I will explore the shell script option.
Thanks again :slight_smile: