0

I would like django to change the reference to the native django table(s) schema(dbo) as seen in the sql below.

I have renamed the schema to meta in SQL, but when I run the app, it recreates the tables on the dbo schema again in the DB.

So I deleted them again and renamed my meta schemas back to dbo in TSQL and tried to get django to migrate them to the new schema name by running,

python manage.py inspectdb > models.py

The models.py generates and then changing the following in the models.py file in the root folder. eg.

class AuthGroup(models.Model):
    # model fields...

    class Meta:
        db_table = 'meta.auth_group'

Then I run makemigrations and then migrate does not detect the models.py changes.

Is this the correct approach or are we still stuck in changing the user running django to have a default schema to be set to "meta"?

ALTER SCHEMA dbo TRANSFER [meta].[django_admin_log] 
ALTER SCHEMA dbo TRANSFER [meta].[admin_interface_theme]
ALTER SCHEMA dbo TRANSFER [meta].[auth_group]
ALTER SCHEMA dbo TRANSFER [meta].[auth_group_permissions]
ALTER SCHEMA dbo TRANSFER [meta].[auth_user]
ALTER SCHEMA dbo TRANSFER [meta].[auth_user_groups]
ALTER SCHEMA dbo TRANSFER [meta].[auth_user_user_permissions]
ALTER SCHEMA dbo TRANSFER [meta].[django_content_type]
ALTER SCHEMA dbo TRANSFER [meta].[django_migrations]
ALTER SCHEMA dbo TRANSFER [meta].[django_session]
ALTER SCHEMA dbo TRANSFER [meta].[reversion_revision]
ALTER SCHEMA dbo TRANSFER [meta].[reversion_version]
ALTER SCHEMA dbo TRANSFER [meta].[auth_permission]
Farrel
  • 185
  • 1
  • 10
  • What's wrong with setting the default schema of the database user that Django is configured to use? – AlwaysLearning Jul 10 '23 at 12:38
  • Less administrative hassles for the clients when deploying or adding new service/user accounts. Would be preferable to manage this within the code, rather than adapting your DB user profiles to work with the application. – Farrel Jul 10 '23 at 13:33
  • I don't understand. You need to create a Login and Database User for the application to use. It's quite literally [a parameter on `CREATE USER`](https://learn.microsoft.com/en-us/sql/t-sql/statements/create-user-transact-sql#b-creating-a-database-user-with-a-default-schema) to specify its default schema. Or are you doing something dangerous like using the `sa` account for your application? – AlwaysLearning Jul 10 '23 at 21:14
  • We know it is as simple and easy. Having multiple clients/environments, new developers and DB Admins restoring UAT/TEST/DEV environments, they constantly need to update the service accounts after a restore. It becomes an extra step in the process of a very large environment and frequently run into something not working and then pointing out the obvious. My question is rather how to do this differently and not why we should use default schema, unless this is the only way? The process I mentioned above, seems to have merit, but not having any luck yet. – Farrel Jul 11 '23 at 09:44

0 Answers0