Search code examples
djangogitgitignore

gitignore how to exclude a table


I have a question.

So I'm working on my Django project for school and it's almost done.

And also, I have to include a .gitignore file which looks like this:

venv/*
.idea/*
db.sqlite3

Nothing fancy just simple. My problem here is, that I have a table in db.sqlite3, and I need those 3 entries in my project. So when somebody merges my project, the entries in this table should exist. Otherwise, you just have an emtpy table and then my function on my Website is not working.

I already googled for this, but I just found how to exlude files or folders. So I hope there is a solution.


Solution

  • If you want your sqlite database to be part of your repository, proceed as follows:

    • edit the file .gitignore
    • remove or comment out the line db.sqlite3
    • add your database to the repository: git add db.sqlite3
    • commit your changes: git commit -m'Add db.sqlite3 to the repository'
    • push your changes so your colleagues get your database: git push

    Although these steps answer your question, I think that doing this is a very bad idea and I really encourage you to not include your sqlite database in the repository.

    There is a reason why the guys at Django put that file in the .gitignore. If you are developing in your local machine, whatever you do that affects the database (create or modify tables, add, remove or update records. etc) will create a modified version of db.sqlite3 that you will commit and push to the remote repository.

    Now, let's suppose there is somebody else collaborating in the project with you. If your collaborator edits records, adds tables or does whatever thing that modifies his local database, he will have changes in the file db.sqlite3. If he tries to merge his commits with your commits, you will have a conflict in the file db.sqlite3 that will be very difficult (if not impossible) to solve.

    I don't use Django, so I cannot help you there, but what you need is a way to populate (or seed) the database. @iklinac gave you a link in the comments about how to create initial data for a Django project.

    What you commit to the repository is the way to populate the database for the first time, but you keep your local databases out of the repository.