Skip to content
Advertisement

Django – fresh database and no such table

I’m using my complex jobs app in production and it works fine, with sqlite database. I’m trying to create new database from scratch and I cannot do that nor using migrations nor trying to make them once again:

When I’m trying to reuse my migrations:

JavaScript

When I’ve tried to remove all migration files:

JavaScript

How can I investigate what is going on? Where is the problem with jobs_role table?

It figures out, that problem is related to my admin.py file

JavaScript

The extra and max_num fields cause the issue. How can I handle that in a correct manner?

Advertisement

Answer

As the error indicates:

JavaScript

This meansthat when interpreting the file, it will already run this query. This is thus also the case if the database is not (yet) fully migrated, hence the error.

As a (strong) rule of thumb, you should try to avoid running queries when interpreting the file (reading the file into memory), since these will thus run before the server starts running, and before the database might be properly set up. Even if that would work, it would mean that extra takes as value the number if items when you start the server: if you would later add/remove a role, then extra would not change.

Usually you should try to wrap the logic into a function such that it only will evaluate Role.objects.all() when it runs the function, and such admin functions are then typically only triggered when the user visits the admin pages.

As for this specific case, you can use the .get_extra(…) [Django-doc] and .get_max_num(…) methods [Django-doc]:

JavaScript
Advertisement