Django unable to open sqlite on SOME queries only? - python

I have had no troubles locally, but pushing a new project to an existing machine (one running plenty of other django apps without trouble) gave this:
OperationalError: unable to open database file
What is more perplexing is:
The sqlite file is read-write for all
This error only happens on some queries! Other's are fine.
In a fresh db after running syncdb, my views work, but /admin/ triggers this.
If I loaddata for some of my apps from data dumped from my local machine, some of the apps trigger this in my views, and others do not.
I can find no correlation between any of the things that seem to trigger this.
Why would it fail to open the database, aside from permissions?

Write access on the directory. I thought i checked that first :-/

Related

Cannot makemigrations on Django Settings already configured error

I'm a new django developer and I recently started a project, and am trying to get the database up and running.
For background: we are using a mysql database for storage, and the settings are configured for it. Using an Unbuntu DO server. I have also been using the virtual environment to run the commands below.
When we initially created the project we ran the makemigrations command, which populated the database with django tables, but not the Models, as we had not made any Models yet. I've now uploaded the Model code, however I could not make any new migrations, I got the error RuntimeError("Settings already configured").
According to this tutorial which I was following (https://www.geekinsta.com/how-to-customize-django-user-model/) some errors are normal and required me to delete and recreate the mysql database (which I did, it has the same name), and clear past migrations (the app/migrations/pycache is now empty). However I'm still experiencing the same error ("Settings already configured").
How can I fix this? I need to successfully run the makemigrations and migrate commands to populate the database. The new database and pycache are empty after I attempted the commands again. Thanks for any and all help.

My Blog Posts disappear everytime I make migrations. Heroku-Django blog

So I made a blog with django and hosted it on heroku(Free version), and every time I make new migrations, the posts that I uploaded earlier would disappear and left are the trial posts which I uploaded on my machine for testing. I don't understand why this is happening.
These are the commands I used.
heroku run python manage.py makemigrations
and then
heroku run python manae.py migrate
I don't think that it is any of the code that is throwing this problem. But if you need to check any of the files, I'll attach'em . Please help.
It's probable that you are still using SQLite, which is stored on disk. Django apps start off with SQLite by default, so if you haven't consciously changed your database that's certainly what you're using. Heroku's active storage is "ephemeral", meaning it goes away when you restart your dynos. This means that while your database will still look the same (because you haven't changed your models or migration files, after all), there won't be any data in it except stuff added after the dyno restarts.
The solution is to use a different database, such as PostGreSQL, that has its own server and lives independent of your Heroku dynos. You can learn more here.

Migrations error in Django after moving to new server

I'm developing a Django 1.8 application locally and having reached a certain point a few days ago, I uploaded the app to a staging server, ran migrations, imported the sql dump, etc. and all was fine.
I've since resumed local development which included the creation of a new model, and changing some columns on an existing model. I ran the migrations locally with success, but after rsync-ing my files to the staging server, I get a 'relation already exists' error when running manage.py migrate. And when I visit the admin page for the new model, I get a 'column does not exist' error.
It seems as though the migrations for this model were partially successful but I cannot migrate the entirety of the model schema. I've tried commenting out parts of the migration files, but was not successful. Would it be possible to create the missing columns via psql? Or is there some way of determining what is missing and then manually write a migration to create the missing database structure?
I'm using Django 1.8.6, Python 3.4.3, and PostgreSQL 9.3.6. Any advice on this would be great. Thanks.
Try running migrate --fake-initial since you're getting the "relation already exists" error. Failing that, I would manually back up each one of my migration folders, remove them from the server, then re-generate migration files for each app and run them all again from scratch (i.e., the initial makemigrations).

Strange error during initial database migration of a Django site

I have been working on a localhost copy of my Django website for a little while now, but finally decided it was time to upload it to PythonAnywhere. The site works perfectly on my localhost, but I am getting strange errors when I do the initial migrations for the new site. For example, I get this:
mysql.connector.errors.DatabaseError: 1264: Out of range value for
column 'applied' at row 1
'applied' is not a field in my model, so this error has to be generated by Django making tables for its own use. I have just checked in the MySQL manager for my localhost and the field 'applied' appears to be from the table django_migrations.
Why is Django mishandling setting up tables for its own use? I have dropped and remade the database a number of times, but the errors persist. If anyone has any idea what would cause this I would appreciate your advice very much.
My website front end is still showing the Hello World page and the Admin link comes up with a page does not exist error. At this stage I am going to assume this is related to the database errors.
EDIT: Additional information about why I cannot access the front-end of the site:
It turns out when I am importing a pre-built site into PythonAnywhere, I have to edit my wsgi.py file to point to the application. The trouble now is that I don't know exactly what to put there. When I follow the standard instructions in the PythonAnywhere help files nothing seems to change. There website is also seems to be very short on detailed error messages to help sort it out. Is there perhaps a way to turn off their standard hello world placeholder pages and see server error messages instead?
As it says in my comment above, it turns out that the problem with the database resulted from running an upgrade of Django from 1.8 to 1.9. I had forgotten about this. After rolling my website back to Django 1.8, the database migrations ran correctly.
The reason why I could not access the website turned out to be because I had to edit the wsgi.py file, but I was editing the wrong version. The nginx localhost web server I was using keeps it in the different folder location than PythonAnyhwere's implementation. I uploaded the file from my localhost copy and edited it according to the instructions on PythonAnywhere's help system without realizing it was not being read by PythonAnywhere's server. What I really needed to do was edit the correct file by accessing it through the web tab on their control panel. Once I edited this file, the website front end began to work as expected.
This issue occurred for me as well on version 1.10 with a brand new project. I found that if you use the recommended driver and the connector in the documentation, the migration works without issues.
If you don't feel like reading the docs, in breif:
Install MySQLdb for python 2.7 or mysqlclient for python 3.3+
Modify your settings.py file. In the DATABASES dictionary set:
'ENGINE': 'django.db.backends.mysql',

sqlite3 -- can't open database file

For background, I'm trying to migrate my sqlite3 local db to a postgresql db on Heroku.
I have no problem accessing the db locally through my (Django) development server. However, when I try to push the db to Heroku, it says it can't open the file.
heroku db:push sqlite://path/to/db --confirm my-app-name
I get the following message:
Loaded Taps v0.3.23
Warning: Data in the app 'my-app-name' will be overwritten and will not be recoverable.
Failed to connect to database:
Sequel::DatabaseConnectionError -> SQLite3::CantOpenException: could not open database: unable to open database file
Most everything I've seen on Google relates to some bugs in earlier versions of Tap. Otherwise, I'm not sure what I should do here.
I've tried following the advice of this question and others about permissions, but I have full read and write access to the file and containing folder. I'm not too experienced with permissions -- do I need to switch ownership of the db to another user?
If /path/to/database is an absolute path, you need to do:
heroku db:push sqlite:///path/to/db --confirm my-app-name
Note the third slash. It could also be a permissions issue, in which case you want to either change the owner to the current user, or give at least read permission to the database (644).
I would try to save the data from sqlite3 to a fixture via dumpdata and then, after switching to postgres, load it again via loaddata.

Categories