# Django database for big data

**URL:** <https://forum.djangoproject.com/t/django-database-for-big-data/9544>\
**Category:** Using Django\
**Created:** [September 3, 2021, 6:41am UTC](https://forum.djangoproject.com/t/django-database-for-big-data/9544 "2021-09-03T06:41:53Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![smailt](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/smailt/32/8270_2.png) [@smailt](https://forum.djangoproject.com/u/smailt)\
**Post date:** [September 3, 2021, 6:41am UTC](https://forum.djangoproject.com/t/django-database-for-big-data/9544/1 "2021-09-03T06:41:53Z")

</div>

Hello,  
is there any way to use two SQLite databases in the same project?

I have big data with many tables so I want to separate the database in my Django project.

For example, in this table, I have 125622 rows and 15 columns.

![acccc](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/2X/2/24ce39ae3c445415b93d5e3cd860813b04da5692.jpeg)

Could I use access DB with Django for such data?  
because I need this format to be loaded in other software.  
or could I export or add new entries to an existing access DB using Django?  
Thanks

---

<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:** [September 3, 2021, 1:28pm UTC](https://forum.djangoproject.com/t/django-database-for-big-data/9544/2 "2021-09-03T13:28:48Z")

</div>

> [@smailt](#):
>
> For example, in this table, I have 125622 rows and 15 columns

This is not “big data”. Multiply that by 10 and you’re starting to get close to something you could call big data.

See [Multiple databases | Django documentation | Django](https://docs.djangoproject.com/en/3.2/topics/db/multi-db/) for handling multiple databases. You can even access different types of databases using that facility. (For example, I’ve got a couple of systems where the Django data is in sqlite, but the “real” data is in PostgreSQL. The system handles both of them seamlessly.)

Django does not natively support Access DBs, but you might be able to find something that works. (Don’t personally know, have never tried.) See [Databases | Django documentation | Django](https://docs.djangoproject.com/en/3.2/ref/databases/) for the official list of supported databases.

---

<div class="post-metadata">

**Author:** ![smailt](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/smailt/32/8270_2.png) [@smailt](https://forum.djangoproject.com/u/smailt)\
**Post date:** [September 3, 2021, 2:58pm UTC](https://forum.djangoproject.com/t/django-database-for-big-data/9544/3 "2021-09-03T14:58:34Z")

</div>

> [@KenWhitesell](#):
>
> 10 and you’re st

Many thanks for the responses

(for big data the example was just one table of many tables I just entered one table on the SQLite file that reaches 20Mb size, and I have tens of such table) .

I followed the django instructions for additionelle database, and in my settings.py I added:

```auto
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    },
    'measure': {
        'NAME': 'user_data',
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'measure.sqlite3'),
    }
}

```

And I run this command :

`./manage.py migrate --database=measure`

So I created the new database, but it cloned all the old db tables?  
I don’t need to creat another copy of tables, how to add and use the new database in modes.py, views.py like the old one even in the Admin page?

 ![mesu](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/2X/2/2fa0e17de3a24cd1a2ec3f714b3ed55f73ece466.jpeg)

My best

---

<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:** [September 3, 2021, 3:06pm UTC](https://forum.djangoproject.com/t/django-database-for-big-data/9544/4 "2021-09-03T15:06:23Z")

</div>

See the Synchronizing your Databases and Automatic database routing sections on that same doc page. (You probably should read the entire page.)

---

<div class="post-metadata">

**Author:** ![smailt](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/smailt/32/8270_2.png) [@smailt](https://forum.djangoproject.com/u/smailt)\
**Post date:** [September 4, 2021, 2:15pm UTC](https://forum.djangoproject.com/t/django-database-for-big-data/9544/5 "2021-09-04T14:15:55Z")

</div>

> [@KenWhitesell](#):
>
> u probably should read the entire pa

I had created a new database and configured my files as in the Django documentation.  
now I can access to the database via the Admin page and add some tables and data.  
this is my settings.py configurations.

```auto
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    },
    'measure_db': {
        'NAME': 'measure_name',
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'measure.db.sqlite3'),
    }
}

DATABASE_ROUTERS =['routers.db_routers.AuthRouter','routers.db_routers.Measure']

```

and I created a folder (routers) in it another file db\_routers.py that contains this code:

```auto

from os import read
class AuthRouter:
    route_app_labels = {'auth', 'contenttypes','sessions','admin'}

    def db_for_read(self, model, **hints):
        if model._meta.app_label in self.route_app_labels:
            return 'default'
        return None
    def db_for_write(self, model, **hints):
        if model._meta.app_label in self.route_app_labels:
            return 'default'
        return None
    
    def allow_relation(self, obj1, obj2, **hints):
        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):
        if app_label in self.route_app_labels:
            return db == 'default'
        return None

class Measure:
    route_app_labels = {'measure'}
    def db_for_read(self, model, **hints):
        if model._meta.app_label in self.route_app_labels:
            return 'measure_db'
        return None
    def db_for_write(self, model, **hints):
        if model._meta.app_label in self.route_app_labels:
            return 'measure_db'
        return None
    def allow_migrate(self, db, app_label, model_name=None, **hints):
        if app_label in self.route_app_labels:
            return db == 'measure_db'
        return None

```

then I created a new app `measure` and in its `models.py` I add this table

```auto
class SurveillanceDesPuits(models.Model):
    PUITS= models.CharField(max_length=15)
....
.....
....
    post_date = models.DateTimeField(default=timezone.now)
    def __str__ (self):
        return self.PUITS

```

I saved in the `admin.py`

then I run :

```auto
> python manage.py makemigrations
> python manage.py migrate --database=measure_db

```

so the same issue it creates a new database named measure.db but it creates all tables in the first database+ the new table named `SurveillanceDesPuits`?  
could I delete those tables manually or there are another tips and tricks?

 ![rerer](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/2X/9/92b001db84097a19ed9063f5b01f8c1f07e7d55f.jpeg)

All my best

---

<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:** [September 4, 2021, 3:09pm UTC](https://forum.djangoproject.com/t/django-database-for-big-data/9544/6 "2021-09-04T15:09:31Z")

</div>

Looking at your allow\_migrate function(s):

> [@smailt](#):
>
> ```auto
> def allow_migrate(self, db, app_label, model_name=None, **hints):
> if app_label in self.route_app_labels:
> return db == 'default'
> return None
> 
> ```

> [@smailt](#):
>
> ```auto
> def allow_migrate(self, db, app_label, model_name=None, **hints):
> if app_label in self.route_app_labels:
> return db == 'measure_db'
> return None
> 
> ```

If you have any apps that aren’t listed in `route_app_labels`, they will be migrated in both - lets say you have a new app called “new\_app”. Since it’s not in either `route_app_labels` set, both `if` statements will be false, meaning both functions will return None - in which case the migration will be allowed to proceed.

---

<div class="post-metadata">

**Author:** ![smailt](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/smailt/32/8270_2.png) [@smailt](https://forum.djangoproject.com/u/smailt)\
**Post date:** [September 4, 2021, 7:49pm UTC](https://forum.djangoproject.com/t/django-database-for-big-data/9544/7 "2021-09-04T19:49:12Z")

</div>

> [@smailt](#):
>
> `python manage.py migrate --database=measure_db`

so I added the unwanted database to route\_app\_labels as:

```auto
route_app_labels = {'auth', 'contenttypes','sessions','admin','Home'}

```

I checked and it works very well.

 ![home](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/2X/d/da5ee1e44fd2bd3f845f49657acbeafdfcbb128b.jpeg)

however, in the new database, there is a migration table that is logical and is related to the new database’s migration. but the new user\_profile table what is its role?

Thanks a lot Mr Ken

---

<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:** [September 5, 2021, 12:29am UTC](https://forum.djangoproject.com/t/django-database-for-big-data/9544/8 "2021-09-05T00:29:37Z")

</div>

I don’t find a “Profile” class in any of the standard packages. It’s either being created in one of your modules or by some other 3rd party package.

---

<div class="post-metadata">

**Author:** ![smailt](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/smailt/32/8270_2.png) [@smailt](https://forum.djangoproject.com/u/smailt)\
**Post date:** [September 5, 2021, 6:30am UTC](https://forum.djangoproject.com/t/django-database-for-big-data/9544/9 "2021-09-05T06:30:30Z")

</div>

> [@smailt](#):
>
> `{'auth', 'contenttypes','sessions','admin',`

yes, you are right, think I should add all apps in `route_app_labels`, I have a `user` app.
