Why is Django migrate command not creating tables in database for specific app?

464 views Asked by At

I've an app named Attendance and it contains the following migrations applied in db.

attendance
 [X] 0001_initial
 [X] 0002_delete_leave
 [X] 0003_alter_holiday_options_alter_shift_options
 [X] 0004_delete_holiday_alter_shift_holidays
 [X] 0005_delete_shift
 [X] 0006_initial
 [X] 0007_alter_leave_options
 [X] 0008_alter_leave_comment_alter_leave_is_regularized
 [X] 0009_attendance
 [X] 0010_alter_attendance_options_attendance_created_at_and_more
 [X] 0011_attendance_status
 [X] 0012_attendance_is_regularized
 [X] 0013_alter_attendance_is_regularized
 [X] 0014_remove_attendance_date_attendance_start_date_and_more
 [X] 0015_attendance_end_date
 [X] 0016_alter_attendance_end_date
 [X] 0017_alter_attendance_end_date_and_more
 [X] 0018_leavetype_remove_leave_half_day_and_more
 [X] 0019_leave_leave_type

when I run python manage.py migrate with or without app label it does not create tables in db.

I've not set managed=False and tried to delete all migrations from django_migrations table but no luck.

One more point I forgot to add that I'm using django-tenants

Update

To debug the issue, I deleted my local database and kept migrations. Before migrating I ran migrate command with --plan flag

python manage.py migrate --plan

Above command gives me this error

[standard:public]     Change Meta options on todo
[standard:public] todo_task.0004_todo_created_at_todo_updated_at

[standard:public]     Add field created_at to todo
[standard:public]     Add field updated_at to todo
Traceback (most recent call last):
  File "env\lib\site-packages\django\db\backends\utils.py", line 89, in _execute
    return self.cursor.execute(sql, params)
psycopg2.errors.UndefinedTable: relation "tenant_tenant" does not exist
LINE 1: SELECT "tenant_tenant"."schema_name" FROM "tenant_tenant" WH...

...

2

There are 2 answers

0
Ankit Tiwari On BEST ANSWER

I found the issue, according to the documentation. All the apps that we want to have their tables in the public schema, we've got to add them to SHARED_APPS.

So I added attendance and task apps in SHARED_APPS.

SHARED_APPS = (
    'django_tenants',  # mandatory
    'attendance',
    'task',
    'django.contrib.contenttypes',

    # everything below here is optional
    'django.contrib.auth',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.admin',
)

After this, I deleted my local database, created a new one, and ran the migrate command.

python manage.py migrate

It worked!

Thanks to all who tried to help me (:

1
user16171413 On

Hope this helps ~

You can reset to your last known migration by using this command

python manage.py migrate app_name migration_name

For example: python manage.py migrate myapp 0001

And then run your migrate command:

python manage.py migrate app_name 

If this doesn't work, you can use the --fake flag to manipulate the current migration state.

python manage.py migrate --fake myapp 0001

This is intended for more experienced users. Kindly see more here https://docs.djangoproject.com/en/dev/ref/django-admin/#migrate