Opened 10 years ago

Closed 10 years ago

#22898 closed New feature (wontfix)

Specify backend name for custom SQL

Reported by: shaung Owned by: nobody
Component: Core (Management commands) Version: dev
Severity: Normal Keywords: custom sql
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The problem:

Say I have a model named AwesomeModel which is managed but several functions need to be created along with the table.

So I created a awesomemodel.sql and put all the DDLs in it. In this way when running unittests the functions get created without problem.

Now as there are multiple DB backends defined in the settings, I'd like the custom SQL run only for the default DB but not for another_schema:

DATABASES = {
     "default": {
         "ENGINE": "django_pyodbc",
         # ...
     },
     "another_schema": {
         "ENGINE": "django_pyodbc",
         # ...
     }
}

It seems that custom SQLs can be specified to be executed for certain engines:

https://github.com/django/django/blob/master/django/core/management/sql.py#L221

    # Find custom SQL, if it's available.
    backend_name = connection.settings_dict['ENGINE'].split('.')[-1]

But for multiple backends that use the same engine, there is no way to distinguish from them.

So changing the file name to model_name.django_pyodbc.sql does not solve this problem.

Suggestion:

Add a new file custom SQL file name pattern to allow specify the target backend by backend name:

<Model Name>.<Engine Name>.<Backend Name>.sql

(In my case it would be awesomemodel.django_pyodbc.default.sql)

Change History (2)

comment:1 by shaung, 10 years ago

Version: 1.6master

comment:2 by Claude Paroz, 10 years ago

Resolution: wontfix
Status: newclosed

As you can read in the documentation, custom SQL is deprecated.
https://docs.djangoproject.com/en/dev/howto/initial-data/#providing-initial-sql-data

The recommended path now is to use data migrations, where you should be able to consult settings and run (SQL) code accordingly.

Note: See TracTickets for help on using tickets.
Back to Top