0

I deleted my database and all my migrations and would like to re-build them from scratch.

When I run makemigrations I get the error:

django.db.utils.OperationalError: no such table: registration_setting

I was able to trace this to my urls.py which reference several views. If I comment this out it builds the migrations fine.

urlpatterns = [
    url(r'results', views.results, name='results'),
    url(r'all', views.results_all, name='results_all'),
    url(r'results_batch', views.results_batch, name='results_batch'),
    url(r'form', views.RegistrationFormView.as_view(), name='reg_form'),
    url(r'^$', views.login, name='login'),
    url(r'tokensignin', views.token_sign_in, name='token_sign_in'),
    url(r'logout', views.logout, name='logout'),
    url(r'^ajax/get_course_description$', views.get_course_description, name='get_course_description'),
    url(r'^not_registered_report', views.not_registered_report, name='not_registered_report'),
]

As best as I can tell it is this line from views.py

settings_CGPS_REG_TERM = Setting.objects.filter(key="CGPS_REG_TERM").first().value

Which in-fact references the Settings table which has not been built yet by migrate. But isn't this a very normal situation? Of course I have code that references values to fetch from tables in my database. Why is makemigrations trying to execute any code whatsoever before creating the underling database tables? Is there some best practice I am not following that is causing this?

3
  • You can not import models in the settings.py file: the settings.py file is one of the first modules that is loaded, long before the models.py of the apps. Commented Jan 13, 2022 at 18:40
  • I am not importing any models in settings.py. I have a table in my models.py called "setting". Commented Jan 13, 2022 at 19:58
  • Well it looks like you are running the code outside of the view methods, which means it will run immediately when the view is interpreted, and this will also happen if you want to make migrations, but at that time; there is no such table in the database yet. You should "postpone" running such queries, and do this in view functions. Commented Jan 13, 2022 at 20:00

1 Answer 1

1

I suspect the line in the views (settings_CGPS_REG_TERM ...) is outside of any function, and is executed when django load the views to validate the python code, before running the migrations.

I think the best approach is a class method to get this value when you need it in your code. Next, you can enhance it with a cache.

from django.core.cache import cache


CGPS_REG_TERM = "CGPS_REG_TERM"  # to avoid typo errors


class Setting(models.Model):
    # ...

    @classmethod
    def get_CGPS_REG_TERM(cls):
        val = cache.get(CGPS_REG_TERM)
        if not val:
            # next line fails if dont exists any object with the key
            val = cls.objects.filter(key=CGPS_REG_TERM).first().value
            cache.set(CGPS_REG_TERM, val, an_optional_timeout_in_seconds)
        return val



def my_view(request):
    method_that_needs_a_setting(Setting.get_CGPS_REG_TERM())
    # ...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.