Search code examples
djangooperationalerror

Django - no such table - custom sql


I don't understand why Django can access my table just fine when I use its built-in query functions, ie. Perk.objects.all(), but the moment I try to use custom sql, it says there is no such table.

def sql(self):
    queryset = Perk.objects.all() # works fine
    cursor = connection.cursor()
    cursor.execute("SELECT * FROM perk")  # Fails. No such table: perk.
    ...

Solution

  • The default django naming convention for tables is "appname_classname". You can read about changing the defaults here:

    Django table naming convention. Can I change its behavior?

    Or look in the docs:

    https://docs.djangoproject.com/en/1.9/ref/models/options/

    And you might peek in your database to see what the table is actually called.