Snowflake-Labs / django-snowflake

MIT License
59 stars 15 forks source link

Error: The session does not have a current database #45

Closed QuinnFreedman closed 2 years ago

QuinnFreedman commented 2 years ago

When I try to run migrate for the Snowflake database, I get the following error:

django.db.migrations.exceptions.MigrationSchemaMissing: Unable to create the django_migrations table
  (Cannot perform CREATE TABLE. This session does not have a current database. Call 'USE DATABASE', or use a qualified name.)

It seems like a database is not being selected before trying to create a table. My DATABASES config looks like this:

DATABASES = {
    'default': ...
    'snowflake': {
        'ENGINE': 'django_snowflake',
        'NAME': '<my database name>',
        'SCHEMA': '<my schema name>',
        'WAREHOUSE': 'COMPUTE_WH',
        'USER': '<snowflake username>',
        'PASSWORD': '<snowflake password>',
        'ACCOUNT': '<snowflake account locator>',
    }
}

I tried adding database to OPTIONS like this:

DATABASES = {
    'default': ...
    'snowflake': {
        ...,
        'OPTIONS': {
            'database': '<my database name>',
        }
    }
}

but it didn't help.

Have I configured something wrong? Or is this an issue with django-snowflake? Does django-snowflake support migrations and creating tables?

timgraham commented 2 years ago

Your first configuration looks okay. Do you have the same issue if Snowflake is the default database? Cedar is no longer contracting me to support this package but if you wish to hire me to debug your issue and/or write a fix if it turns out to be bug in this pacakge, feel free to reach out.

timgraham commented 2 years ago

The cause was incorrect Snowflake permissions and/or database does not exist.

jm7647 commented 1 year ago

Having identical issue - and it's not permissions nor database issue:

File "/home/jm/django-test/django_env/lib/python3.10/site-packages/django/db/migrations/recorder.py", line 72, in ensure_schema raise MigrationSchemaMissing( django.db.migrations.exceptions.MigrationSchemaMissing: Unable to create the django_migrations table (Cannot perform CREATE TABLE. This session does not have a current database. Call 'USE DATABASE', or use a qualified name.)

timgraham commented 1 year ago

When I debugged this for the original reporter, it might be that ALTER USER django_user SET DEFAULT_ROLE="SYSADMIN" was helpful. How did you confirm your permissions are correct? Are you able to execute queries with snowflake-connector-python?

jm7647 commented 1 year ago

Yes, I can run queries with snowflake connector, no issues. The problem I noticed is exactly what error says. When I open DB shell this is what I get:

python manage.py dbshell

although when I just hit ENTER key this is what I get:

Apparently open session has no clue about warehouse, database and schema listed in the first prompt.

timgraham commented 1 year ago

Try running SHOW DATABASES; at the dbshell prompt. If you can't see your target database, you know you have a permissions issue. If that's the case, you may find this StackOverflow answer helpful.

jm7647 commented 1 year ago

You are absolutely correct, when I used different user it all works well.