Why doesn't a django sqlite3 database work the same on one machine vs the other?

Viewed 147

I'm working on learning Django, but I've run into to a problem with my database not transferring correctly (or it's transferring correctly and I'm missing something, which is what I think is happening).

I have a Django model Link in models.py

class Link(models.Model):
    title = models.CharField(max_length=100)
    alt = models.CharField(max_length=100)
    dest = models.CharField(max_length=100)
    image = models.FilePathField(path='static/misc/link_in_bio/img/')
    def __str__(self):
        title = self.title + ' Link'
        return self.title
    class Meta:
        ordering = ('title', )

Then I made instances of this model and confirmed they are entered into the database correctly. They are then referenced in a page's html like so:

{% for link in links %}
<div id="space">
    <div class="w3-container">
        <div class="w3-panel" style="padding: 0.06em">
            <button class="block" type="button" title="{{link.title}}" onclick="window.open('{{link.dest}}')">
                <img src="../{{link.image}}" alt="{{link.alt}}" style="max-height: 7em">
            </button>
        </div>
    </div>
</div>
{% endfor %}

On my development machine everything works exactly as expected and shows this: correct screen

Then, when I deploy it on Heroku, everything builds correctly, python manage.py makemigrations and python manage.py migrate run correctly, but when I view the page it simply shows this:incorrect screen

I also can't login to the admin page with the superuser I already created on the first machine, and I know this is related but I'm not sure how to approach it. Is there some step to load and initialize the database or something?

Full code is on github here if it helps.

Thanks for the help.

2 Answers

That happens because their architecture filesystem isn't good for SQLite3 and results in no data at all on a regular basis, making your database empty.

The Heroku Dev Center has an entire page on the subject. Quoting the relevant portions:

Heroku’s Cedar stack has an ephemeral filesystem. You can write to it, and you can read from it, but the contents will be cleared periodically. If you were to use SQLite on Heroku, you would lose your entire database at least once every 24 hours.

Even if Heroku’s disks were persistent running SQLite would still not be a good fit. Since SQLite does not run as a service, each dyno would run a separate running copy. Each of these copies need their own disk backed store. This would mean that each dyno powering your app would have a different set of data since the disks are not synchronized.

Heroku suggests to use PostgreSQL in production, and you will find a lot of resources talking about how to use it instead of SQLite on Heroku.

You can also use the platform PythonAnywhere that supports SQLite3 if you want.

Use postgreSQL in development environment. If you do not want to get dirty your development environment just try with a docker container:

https://hub.docker.com/_/postgres

docker run --name postgres -v postgresql-data:/var/lib/postgresql/data -e POSTGRES_PASSWORD=mysecretpassword -d postgres

So, your development environment will be closer to production (try to use the same version too).

Related