2

I'm building a website with Django and I need to have a GPS coordinate attribute in my User model.

I will be using GeoDjango, which needs PostgreSQL and, since my website is already working with the sqlite3 default database, I need to install a PostgreSQL database to the project in order to create a Coordinates model in it and link it to the User model of the SQLite database.

I found lots of tutorials on the internet about "how to use multiple databases" but they are all very basics. Since I'm quite new to Django and web programming, I'm needing an in-depth tutorial about multiple databases. I have a few questions and I thank you in advance if you can answer one or more of those!

Note: I'm using windows 10 with PostgreSQL installed with the ubuntu app of windows.

1) How can I create and initialize the file for the new database in the Django project? The file db.sqlite3 was automatically created with the Django project initialization so how can I create another one?

2) Do I need to use a new app for the GPS coordinates or can I use an existing one?

3) How can I make the OneToOne relationship between my User model and my Coordinates model that will be in the other database?

5
  • 3
    I will answer 3rd: Django does not support cross DB relations. Commented Jan 22, 2020 at 11:56
  • @NalinDobhal so do you have any suggestions about how to reach my purpose ? I hope there are others solutions than deleting everything and rebuilding a project with a postgreSQL database alone Commented Jan 22, 2020 at 12:00
  • 3
    You don't have to delete anything. Just migrate your existing data over to PostgreSQL. It's commonly done, and you can find lots of information about how to do that. A minor inconvenience now that will save you from all the limitations and complications that come with using multiple database engines. Commented Jan 22, 2020 at 12:17
  • @KevinChristopherHenry Thanks I think I will do that ! Commented Jan 22, 2020 at 12:50
  • 2
    @JulienMertz sqlite is barely suited for production anyway. Commented Jan 22, 2020 at 13:55

2 Answers 2

2

As @NalinDobhal mentions in the comments:

Cross-database relations

Django doesn’t currently provide any support for foreign key or many-to-many relationships spanning multiple databases. If you have used a router to partition models to different databases, any foreign key and many-to-many relationships defined by those models must be internal to a single database.

As I see it you have 2 options:

  1. Install SpatiaLite and continue using SQLite for your project, enabling spatial types in your existing DB (follow the documented GeoDjango Instructions on the matter)
  2. Preferred Solution: Migrate your existing SQLite DB to PostgreSQL and enable PostGIS. You can have an excellent read on why this is preferred and how to do the migration correctly through Django in this article.

Long story short for the preferred process:

  • Make a DB dump of the existing DB:

    python manage.py dumpdata > datadump.json
    
  • Enter the Django Shell and delete the existing ContentType data

    python manage.py shell
    
    >>> from django.contrib.contenttypes.models import ContentType
    >>> ContentType.objects.all().delete()
    >>> quit()
    
  • Load the dump file into the PostgreSQL DB:

    python manage.py loaddata datadump.json
    

Note: This migration process is not only SQLite to PostgreSQL specific and can be used in almost every migration between DBs (that I know of ATM).

Sign up to request clarification or add additional context in comments.

Comments

2

You can't just create a database file. except for SQL lite that is a very basic database and can not be used in production, other databases usually come with embedded servers and don't have a file you can see. you can actually use multiple databases either you have models in different apps or you need some specific tables to be in another database. but Django does not support cross-database relations!

see here for more info:

Django 1.11 can we create relationship between tables from two different databases?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.