Skip to main content

Snowflake backend for Django

Install and usage

Use the version of django-snowflake that corresponds to your version of Django. For example, to get the latest compatible release for Django 6.1.x:

pip install django-snowflake==6.1.*

The minor release number of Django doesn't correspond to the minor release number of django-snowflake. Use the latest minor release of each.

Configure the Django DATABASES setting similar to this:

DATABASES = {
    'default': {
        'ENGINE': 'django_snowflake',
        'NAME': 'MY_DATABASE',
        'SCHEMA': 'MY_SCHEMA',
        'WAREHOUSE': 'MY_WAREHOUSE',
        'USER': 'my_user',
        'PASSWORD': 'my_password',
        'ACCOUNT': 'my_account',
        # Include 'OPTIONS' if you need to specify any other
        # snowflake.connector.connect() parameters, documented at:
        # https://docs.snowflake.com/en/user-guide/python-connector-api.html#connect
        'OPTIONS': {
            # Examples:
            'role': 'MY_ROLE',
            # To use native Okta authenticators:
            # https://docs.snowflake.com/en/user-guide/admin-security-fed-auth-use#native-sso-okta-only
            'authenticator': 'https://example.okta.com',
            # To use private key authentication:
            'private_key_file': '<path>/rsa_key.p8',
            'private_key_file_pwd': 'my_passphrase',
        },
    },
}

Persistent connections

To use persisent connections, set Django's CONN_MAX_AGE and Snowflake Python Connector's client_session_keep_alive:

DATABASES = {
    'default': {
        # ...
        'CONN_MAX_AGE': None,
        'OPTIONS': {
            'client_session_keep_alive': True,
        },
    },
}

Snowpark Container Services (SPCS) connections

When your Django app runs inside a Snowpark Container Services (SPCS) service, Snowflake automatically provides an OAuth login token and a host for connecting to Snowflake. django-snowflake detects this environment using the SNOWFLAKE_SERVICE_NAME environment variable that Snowflake sets for every service container. In that case, it:

  • Falls back to the token at /snowflake/session/token if OPTIONS['token'] isn't set. This token is short-lived and is re-read for each new connection.
  • Falls back to the SNOWFLAKE_HOST environment variable if HOST isn't set.
  • Doesn't require USER to be set.

Notes on Django fields

  • Consistent with Snowflake's convention, this backend uppercases all database identifiers (table names, column names, etc.) unless they are quoted, e.g. db_table='"table_name"'.

  • Snowflake supports defining foreign key and unique constraints, however, it doesn't enforce them. Thus, Django manages these constraints and inspectdb detects them, but Django won't raise IntegrityError if they're violated.

  • Snowflake doesn't support indexes. Thus, Django ignores any indexes defined on models or fields.

  • Snowflake doesn't support check constraints, so the various PositiveIntegerField model fields allow negative values (though validation at the form level still works).

  • Only virtual GeneratedField columns (db_persist=False) are supported by Snowflake, not stored ones. In addition, GeneratedField must be defined with null=True since Snowflake doesn't support adding a NOT NULL constraint to virtual columns.

Notes on Django QuerySets

  • Snowflake has limited support for subqueries.

  • Valid values for QuerySet.explain()'s format parameter are 'json', 'tabular', and 'text'. The default is 'tabular'.

Known issues and limitations

This list isn't exhaustive. If you run into a problem, consult django_snowflake/features.py to see if a similar test is skipped. Please create an issue on GitHub if you encounter an issue worth documenting.

  • Snowflake doesn't support last_insert_id to retrieve the ID of a newly created object. Instead, this backend issues the query SELECT MAX(pk_name) FROM table_name to retrieve the ID. This is subject to race conditions if objects are created concurrently. This makes this backend inappropriate for use in web app use cases where multiple clients could be creating objects at the same time. Further, you should not manually specify an ID (e.g. MyModel(id=1)) when creating an object.

  • Due to snowflake-connector-python's lack of VARIANT support, some JSONField queries with complex JSON parameters don't work.

    For example, if value is a JSONField, this won't work:

    >>> JSONModel.objects.filter(value__k={"l": "m"})
    

    A workaround is:

    >>> from django.db.models.expressions import RawSQL
    >>> JSONModel.objects.filter(value__k=RawSQL("PARSE_JSON(%s)", ('{"l": "m"}',)))
    

    In addition, QuerySet.bulk_update() isn't supported for JSONField.

  • Interval math where the interval is a column is not supported.

  • Interval math with a null interval crashes.

Troubleshooting

Debug logging

To troubleshoot issues with connectivity to Snowflake, you can enable Snowflake Connector for Python's logging using Django's LOGGING setting.

This is a minimal addition to Django's default "loggers" configuration that enables the connector's DEBUG logging:

LOGGING = {
    
    "loggers": {
        
        "snowflake.connector": {
            "level": "DEBUG",
            "handlers": ["console"],
        },
    },
}

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_snowflake-6.1.tar.gz (29.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_snowflake-6.1-py3-none-any.whl (30.5 kB view details)

Uploaded Python 3

File details

Details for the file django_snowflake-6.1.tar.gz.

File metadata

  • Download URL: django_snowflake-6.1.tar.gz
  • Upload date:
  • Size: 29.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.13

File hashes

Hashes for django_snowflake-6.1.tar.gz
Algorithm Hash digest
SHA256 0462e5105eeca0049eeaaa42499c45f098d99eaf88c3a7fb00a5872f012ded72
MD5 8b5c6c29669efccc035c58808a51e6c4
BLAKE2b-256 095a9d7c473d7bbd9d867b8459a0a7136935c7a3798650f08abb2f2a3d5eafda

See more details on using hashes here.

File details

Details for the file django_snowflake-6.1-py3-none-any.whl.

File metadata

  • Download URL: django_snowflake-6.1-py3-none-any.whl
  • Upload date:
  • Size: 30.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.13

File hashes

Hashes for django_snowflake-6.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6ff5309bec745cfc373b074e550549d3f95ee177264f9e3481ec888ef4d6a3ac
MD5 922421baee7796b01711b1d70745afa5
BLAKE2b-256 477ec1919fac3436ea5a8ee02fb54c5853315360b83a1fe9f927934b4a6c5923

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

6.1 This release

2 files

6.0.1

2 files

6.0

2 files

5.2

2 files

5.1

2 files

5.0.3

2 files

5.0.2

2 files

5.0.1

2 files

5.0

2 files

4.2.3

2 files

4.2.2

2 files

4.2.1

2 files

4.2

2 files

4.1

2 files

4.0

2 files

3.2

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page