# Error: There is already an object named 'django\_migrations' in the database.

**URL:** <https://forum.djangoproject.com/t/error-there-is-already-an-object-named-django-migrations-in-the-database/33607>\
**Category:** Using the ORM\
**Created:** [August 6, 2024, 11:42am UTC](https://forum.djangoproject.com/t/error-there-is-already-an-object-named-django-migrations-in-the-database/33607 "2024-08-06T11:42:16Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![kelvinho8](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kelvinho8/32/2853_2.png) [@kelvinho8](https://forum.djangoproject.com/u/kelvinho8)\
**Post date:** [August 6, 2024, 11:42am UTC](https://forum.djangoproject.com/t/error-there-is-already-an-object-named-django-migrations-in-the-database/33607/1 "2024-08-06T11:42:16Z")

</div>

Hi everyone,

Given that I have a django project with the following database setup:

‘default’ = sqlite3, mainly used for the storage of user, group, contenttype, migration etc…  
‘mssql’=sql server, mainly used for data storage

both databases need read, write, and migration permission. mssql db is created since I don’t have permissions for database creation in sql server. Otherwise, I’d set mssql as default database.

Initially, the django\_migrations table has been created after running the following

```auto
python manage.py makemigrations
python manage.py migrate

```

Let’s say a model table has been created under an app ‘team\_app’, when I run the following command to get the table migrated to mssql database, the following error came out

```auto
python manage.py migrate --database mssql

```

```auto
Running migrations: 
  Applying contenttypes.0001_initial...

pyodbc.ProgrammingError: ('42501', "[42501] [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]There is already an object named 'django_migrations' in the database. (2714) (SQLExecDirectW")

.
.
.

django.db.migrations.exceptions.MigrationSchemaMissing: Unable to create the django_migrations table (('42501', "[42501] [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]There is already an object named 'django_migrations' in the database. (2714) (SQLExecDirectW")

```

Here is my router.py partial code.

```auto
class CHRouter:
.
.
.
    def allow_migration(self, db, app_label, model_name=None, **hints):
        if app_label=="contenttypes" or model_name=="django_migrations":
            return db=='default'
        elif app_label=='team_app':
            return db=='mssql'
.
.
.

```

Based on the information above, my I ask how to get this error fixed?

Thank you for your response in advance.

Kelvin

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [August 6, 2024, 12:50pm UTC](https://forum.djangoproject.com/t/error-there-is-already-an-object-named-django-migrations-in-the-database/33607/2 "2024-08-06T12:50:37Z")

</div>

If you’re going to use the migration facilities on the mssql database, you need to allow Django to create the `django_migrations` model on _that_ database. The `django_migrations` model exists on a per-database basis. (Otherwise, Django isn’t able to know which migrations have been applied to the database on _that_ database.)

So your basic choices are:

- Remove `django_migrations` from your router
- Don’t use migrations with the `mssql` database.

---

<div class="post-metadata">

**Author:** ![kelvinho8](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kelvinho8/32/2853_2.png) [@kelvinho8](https://forum.djangoproject.com/u/kelvinho8)\
**Post date:** [August 6, 2024, 6:11pm UTC](https://forum.djangoproject.com/t/error-there-is-already-an-object-named-django-migrations-in-the-database/33607/3 "2024-08-06T18:11:27Z")

</div>

Hi Ken,

I found that even though I set `django_migrations` from my router.py as follows, the `django_migrations` was still created in ‘mssql’ database. I wonder whether i set it correctly?

```auto
.
.
    def allow_migrate(self, db, app_label, model_name=None, **hints):
        if model_name=="django_migrations": 
            return db=='default'

```

Kelvin

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [August 6, 2024, 6:25pm UTC](https://forum.djangoproject.com/t/error-there-is-already-an-object-named-django-migrations-in-the-database/33607/4 "2024-08-06T18:25:27Z")

</div>

There is no “all\_migration” method in routers. The method name is [allow\_migrate](https://docs.djangoproject.com/en/5.0/topics/db/multi-db/#allow_migrate), which should return True, False, or None. It does not return a database to use on the migration.

But again, allowing _any_ migrations on a database without a migrations table is not going to work.

---

<div class="post-metadata">

**Author:** ![white-seolpyo](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/white-seolpyo/32/22386_2.png) [@white-seolpyo](https://forum.djangoproject.com/u/white-seolpyo)\
**Post date:** [August 7, 2024, 1:01am UTC](https://forum.djangoproject.com/t/error-there-is-already-an-object-named-django-migrations-in-the-database/33607/5 "2024-08-07T01:01:19Z")

</div>

Are you trying to migrate a db created in another project?

---

<div class="post-metadata">

**Author:** ![kelvinho8](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kelvinho8/32/2853_2.png) [@kelvinho8](https://forum.djangoproject.com/u/kelvinho8)\
**Post date:** [August 7, 2024, 3:58am UTC](https://forum.djangoproject.com/t/error-there-is-already-an-object-named-django-migrations-in-the-database/33607/6 "2024-08-07T03:58:55Z")

</div>

No, I’m trying to add an additional db (new data source) from SQL server to an existing Django project.

---

<div class="post-metadata">

**Author:** ![kelvinho8](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kelvinho8/32/2853_2.png) [@kelvinho8](https://forum.djangoproject.com/u/kelvinho8)\
**Post date:** [August 7, 2024, 4:36am UTC](https://forum.djangoproject.com/t/error-there-is-already-an-object-named-django-migrations-in-the-database/33607/7 "2024-08-07T04:36:04Z")

</div>

Hi Ken,

Sorry for the typo. The function name is allow\_migrate that is used.

And the same error still occurred, even django\_migrations logic has been removed from my router.

One more thing is that the table django\_migrations was created successfully when running `python manage.py migrate --database mssql`

I have modified router.py as follows:

```auto
class AuthRouter:
    """
    A router to control all database operations on models in the
    auth and contenttypes applications.
    """

    route_app_labels = {"auth", "contenttypes"}

    def db_for_read(self, model, **hints):
        """
        Attempts to read auth and contenttypes models go to auth_db.
        """
        if model._meta.app_label in self.route_app_labels:
            return "default"
        return None

    def db_for_write(self, model, **hints):
        """
        Attempts to write auth and contenttypes models go to auth_db.
        """
        if model._meta.app_label in self.route_app_labels:
            return "default"
        return None

    def allow_relation(self, obj1, obj2, **hints):
        """
        Allow relations if a model in the auth or contenttypes apps is
        involved.
        """
        if (
            obj1._meta.app_label in self.route_app_labels
            or obj2._meta.app_label in self.route_app_labels
        ):
            return True
        return None

    def allow_migrate(self, db, app_label, model_name=None, **hints):
        """
        Make sure the auth and contenttypes apps only appear in the
        'auth_db' database.
        """
        if app_label in self.route_app_labels:
            return db == "default"
        return None

```

```auto
class CHRouter:
.
.
.
    def allow_migrate(self, db, app_label, model_name=None, **hints):
        allow_migrate_app_labels = {'mssql_app'}

        if app_label in allow_migrate_app_labels:
            return True
        else:
            return False

```

in settings.py

```auto
.
.
.

DATABASE_ROUTERS = ['myproject.dbrouters.AuthRouter','myproject.dbrouters.CHRouter']
.
.

```

Kelvin

---

<div class="post-metadata">

**Author:** ![white-seolpyo](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/white-seolpyo/32/22386_2.png) [@white-seolpyo](https://forum.djangoproject.com/u/white-seolpyo)\
**Post date:** [August 7, 2024, 4:42am UTC](https://forum.djangoproject.com/t/error-there-is-already-an-object-named-django-migrations-in-the-database/33607/8 "2024-08-07T04:42:19Z")

</div>

add column? or create new table? or create new database?

---

<div class="post-metadata">

**Author:** ![kelvinho8](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kelvinho8/32/2853_2.png) [@kelvinho8](https://forum.djangoproject.com/u/kelvinho8)\
**Post date:** [August 7, 2024, 4:46am UTC](https://forum.djangoproject.com/t/error-there-is-already-an-object-named-django-migrations-in-the-database/33607/9 "2024-08-07T04:46:52Z")

</div>

I’m adding a new database source ‘mssql’ in a django project. A new model table in ‘mssql\_app.models’ is created and needd to migrate to the new database source ‘mssql’.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [August 7, 2024, 4:56am UTC](https://forum.djangoproject.com/t/error-there-is-already-an-object-named-django-migrations-in-the-database/33607/10 "2024-08-07T04:56:43Z")

</div>

If you’re getting an error about a table already existing, you can either drop the table to allow the migration to create it, or run the migration with the `—fake` parameter to allow Django to track that migration as having been applied without actually changing the database. Either option has a set of potential side effects, depending upon the other migrations in that migration file.

I’m kinda losing track here of what the current status of your databases are, it might help if you summarize what that status is.

---

<div class="post-metadata">

**Author:** ![kelvinho8](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kelvinho8/32/2853_2.png) [@kelvinho8](https://forum.djangoproject.com/u/kelvinho8)\
**Post date:** [August 7, 2024, 5:08am UTC](https://forum.djangoproject.com/t/error-there-is-already-an-object-named-django-migrations-in-the-database/33607/11 "2024-08-07T05:08:01Z")

</div>

Actually, I already did and dropped the existing table `django_migrations` in ‘mssql’ database before running `python manage.py migrate --database mssql`, so that means the table `django_migrations` had been created and then the error occurred while applying contenttypes.0001\_initial within a single execution `python manage.py migrate --database mssql`

My current database status are that  
The django\_migrations table already created and existed in ‘default’ sqlite db after initially setup.  
The django\_migrations table is not existed in ‘mssql’ sql server db and I’m trying to add this new database source to my existing django project.

Please let me know if more information is needed.

Thank you so much for your response.

Kelvin

---

<div class="post-metadata">

**Author:** ![white-seolpyo](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/white-seolpyo/32/22386_2.png) [@white-seolpyo](https://forum.djangoproject.com/u/white-seolpyo)\
**Post date:** [August 7, 2024, 5:10am UTC](https://forum.djangoproject.com/t/error-there-is-already-an-object-named-django-migrations-in-the-database/33607/12 "2024-08-07T05:10:08Z")

</div>

mssql database is .sqlite??? not mysql???  
would you add infomation that DATABASES in settings.py without database auth info?

---

<div class="post-metadata">

**Author:** ![kelvinho8](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kelvinho8/32/2853_2.png) [@kelvinho8](https://forum.djangoproject.com/u/kelvinho8)\
**Post date:** [August 7, 2024, 5:31am UTC](https://forum.djangoproject.com/t/error-there-is-already-an-object-named-django-migrations-in-the-database/33607/13 "2024-08-07T05:31:48Z")

</div>

‘mssql’ is a database in SQL server.

would you add infomation that DATABASES in settings.py without database auth info? \< May I ask what you mean? Could you elaborate with an example? Thanks.

---

<div class="post-metadata">

**Author:** ![white-seolpyo](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/white-seolpyo/32/22386_2.png) [@white-seolpyo](https://forum.djangoproject.com/u/white-seolpyo)\
**Post date:** [August 7, 2024, 5:39am UTC](https://forum.djangoproject.com/t/error-there-is-already-an-object-named-django-migrations-in-the-database/33607/14 "2024-08-07T05:39:20Z")

</div>

add “DATABASES” value in your settings.py

---

<div class="post-metadata">

**Author:** ![kelvinho8](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kelvinho8/32/2853_2.png) [@kelvinho8](https://forum.djangoproject.com/u/kelvinho8)\
**Post date:** [August 7, 2024, 6:05am UTC](https://forum.djangoproject.com/t/error-there-is-already-an-object-named-django-migrations-in-the-database/33607/15 "2024-08-07T06:05:00Z")

</div>

Setting DATABASES in settings.py is the first thing I did and both databases have been configured in settings.py

---

<div class="post-metadata">

**Author:** ![white-seolpyo](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/white-seolpyo/32/22386_2.png) [@white-seolpyo](https://forum.djangoproject.com/u/white-seolpyo)\
**Post date:** [August 7, 2024, 6:26am UTC](https://forum.djangoproject.com/t/error-there-is-already-an-object-named-django-migrations-in-the-database/33607/16 "2024-08-07T06:26:54Z")

</div>

please share the values ​​of the DATABASES you set up.

---

<div class="post-metadata">

**Author:** ![kelvinho8](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kelvinho8/32/2853_2.png) [@kelvinho8](https://forum.djangoproject.com/u/kelvinho8)\
**Post date:** [August 7, 2024, 6:45am UTC](https://forum.djangoproject.com/t/error-there-is-already-an-object-named-django-migrations-in-the-database/33607/17 "2024-08-07T06:45:58Z")

</div>

In settings.py

```auto
DATABASES = {
  'default': {
    'ENGINE': 'django.db.backends.sqlite3',
    'NAME': BASE_DIR / 'db.sqlite3',
  }

  'mssql': {
    'ENGINE': 'mssql',
    'NAME': MSSQL_NAME,
    'USER': MSSQL_USER,
    'PASSWORD': MSSQL_PASSWORD,
    'HOST': MSSQL_HOST,
    'PORT': '',
    'OPTIONS': {
      'extra_params': 'Trusted_Connection=Yes',
      'driver': 'ODBC Driver 11 for SQL Server',
    }
  }
}

DATABASE_ROUTERS = ['myproject.dbrouters.AuthRouter','myproject.dbrouters.CHRouter']

```

django\_migrations table was able to create in mssql DB, so I assume both DB connections work just fine.

Kelvin

---

<div class="post-metadata">

**Author:** ![white-seolpyo](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/white-seolpyo/32/22386_2.png) [@white-seolpyo](https://forum.djangoproject.com/u/white-seolpyo)\
**Post date:** [August 7, 2024, 6:50am UTC](https://forum.djangoproject.com/t/error-there-is-already-an-object-named-django-migrations-in-the-database/33607/18 "2024-08-07T06:50:25Z")

</div>

did you try delete msql database and migrate??

---

<div class="post-metadata">

**Author:** ![kelvinho8](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kelvinho8/32/2853_2.png) [@kelvinho8](https://forum.djangoproject.com/u/kelvinho8)\
**Post date:** [August 7, 2024, 6:59am UTC](https://forum.djangoproject.com/t/error-there-is-already-an-object-named-django-migrations-in-the-database/33607/19 "2024-08-07T06:59:54Z")

</div>

I believe deleting a database is not an option to me. I am able to delete tables but not the entire database 🙂

---

<div class="post-metadata">

**Author:** ![white-seolpyo](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/white-seolpyo/32/22386_2.png) [@white-seolpyo](https://forum.djangoproject.com/u/white-seolpyo)\
**Post date:** [August 7, 2024, 7:01am UTC](https://forum.djangoproject.com/t/error-there-is-already-an-object-named-django-migrations-in-the-database/33607/20 "2024-08-07T07:01:59Z")

</div>

You said you were creating a new DB, but why can’t you delete it?

[Next page](https://forum.djangoproject.com/t/error-there-is-already-an-object-named-django-migrations-in-the-database/33607.md?page=2)
