1
resposta

[Bug] [Django] [Django Rest] Erro de migrate no deploy de uma aplicação django rest

Fiz o curso de django react porém só estava testando local, quando coloquei no heroku ocorreu um erro... Fiz o deploy certinho, mas quando vou acessar a url da api ele não encontra as tabelas mesmo eu já tendo executado o migrate no heroku

Mensagem do migrate:

Operations to perform:
  Apply all migrations: admin, auth, contenttypes, restaurante, 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 restaurante.0001_initial... OK
  Applying sessions.0001_initial... OK

Erro no navegador:

OperationalError at /api/pratos/
no such table: restaurante_prato
Request Method:    GET
Request URL:    https://drf-alura-pizza.herokuapp.com/api/pratos/
Django Version:    4.1.1
Exception Type:    OperationalError
Exception Value:    
no such table: restaurante_prato
Exception Location:    /app/.heroku/python/lib/python3.10/site-packages/django/db/backends/sqlite3/base.py, line 357, in execute
Raised during:    restaurante.views.PratoListView
Python Executable:    /app/.heroku/python/bin/python
Python Version:    3.10.7
Python Path:    
['/app/.heroku/python/bin',
 '/app',
 '/app/.heroku/python/lib/python310.zip',
 '/app/.heroku/python/lib/python3.10',
 '/app/.heroku/python/lib/python3.10/lib-dynload',
 '/app/.heroku/python/lib/python3.10/site-packages']
Server time:    Mon, 03 Oct 2022 11:14:17 -0300

O projeto local está funcionando normalmente: imagem da api local

link no github: https://github.com/fehenriq/drf-alura-pizza.git

1 resposta

Erro ao tentar criar superuser após o migrate:

You have 19 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, restaurante, sessions.
Run 'python manage.py migrate' to apply them.
Traceback (most recent call last):
  File "/app/.heroku/python/lib/python3.10/site-packages/django/db/backends/utils.py", line 89, in _execute
    return self.cursor.execute(sql, params)
  File "/app/.heroku/python/lib/python3.10/site-packages/django/db/backends/sqlite3/base.py", line 357, in execute
    return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such table: auth_user

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

Traceback (most recent call last):
  File "/app/manage.py", line 22, in <module>
    main()
  File "/app/manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/app/.heroku/python/lib/python3.10/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
    utility.execute()
  File "/app/.heroku/python/lib/python3.10/site-packages/django/core/management/__init__.py", line 440, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/app/.heroku/python/lib/python3.10/site-packages/django/core/management/base.py", line 402, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/app/.heroku/python/lib/python3.10/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 88, in execute
    return super().execute(*args, **options)
  File "/app/.heroku/python/lib/python3.10/site-packages/django/core/management/base.py", line 448, in execute
    output = self.handle(*args, **options)
  File "/app/.heroku/python/lib/python3.10/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 109, in handle
    default_username = get_default_username(database=database)
  File "/app/.heroku/python/lib/python3.10/site-packages/django/contrib/auth/management/__init__.py", line 163, in get_default_username
    auth_app.User._default_manager.db_manager(database).get(
  File "/app/.heroku/python/lib/python3.10/site-packages/django/db/models/manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/app/.heroku/python/lib/python3.10/site-packages/django/db/models/query.py", line 646, in get
    num = len(clone)
  File "/app/.heroku/python/lib/python3.10/site-packages/django/db/models/query.py", line 376, in __len__
    self._fetch_all()
  File "/app/.heroku/python/lib/python3.10/site-packages/django/db/models/query.py", line 1866, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "/app/.heroku/python/lib/python3.10/site-packages/django/db/models/query.py", line 87, in __iter__
    results = compiler.execute_sql(
  File "/app/.heroku/python/lib/python3.10/site-packages/django/db/models/sql/compiler.py", line 1395, in execute_sql
    cursor.execute(sql, params)
  File "/app/.heroku/python/lib/python3.10/site-packages/django/db/backends/utils.py", line 103, in execute
    return super().execute(sql, params)
  File "/app/.heroku/python/lib/python3.10/site-packages/django/db/backends/utils.py", line 67, in execute
    return self._execute_with_wrappers(
  File "/app/.heroku/python/lib/python3.10/site-packages/django/db/backends/utils.py", line 80, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/app/.heroku/python/lib/python3.10/site-packages/django/db/backends/utils.py", line 84, in _execute
    with self.db.wrap_database_errors:
  File "/app/.heroku/python/lib/python3.10/site-packages/django/db/utils.py", line 91, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/app/.heroku/python/lib/python3.10/site-packages/django/db/backends/utils.py", line 89, in _execute
    return self.cursor.execute(sql, params)
  File "/app/.heroku/python/lib/python3.10/site-packages/django/db/backends/sqlite3/base.py", line 357, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: auth_user