How do I get Django to log why an sql transaction failed? - python

I am trying to debug a Pootle (pootle is build on django) installation which fails with a django transaction error whenever I try to add a template to an existing language. Using the python debugger I can see that it fails when pootle tries to save a model as well as all the queries that have been made in that session.
What I can't see is what specifically causes the save to fail. I figure pootle/django must have added some database database constraint, how do I figure out which one? MySql (the database being used) apparently can't log just failed transactions.

Install django debug toolbar, you can easily check all of the queries that have been executed

Related

Django Custom Commands: Will writing to SQLite DB with a custom command while the server is running fail due to File Lock?

I would like to keep the django server running as I call some custom commands that will read / write to the sqlite DB. However I'm nervous that this may conflict due to the main server locking the database file.
Is this cause for concern ?
I can't use PostgreSQL or MySQL.
I tried this and it didn't throw any errors, however I would like to know if this is something that can occur.

How to solve operationError: no such table

I'm setting up my new website, but the web have some problem. It gives me an OperationalError at /topics/ and no such table: toms_topic.
This is a Heroku web dyno, running Django2.2 and Python 3.7.3.
I have executed
python manage.py migrate
and when I go to the db.sqlite I find the table toms_topic exists.
And everybody can goto the website https://cblingh.herokuapp.com for take more information
My last website haven't this problem,maybe I add image this time,and the image is foreignkey with topic.But i not sure it's the key of this problem yet.The topic have two foreignkey (entry,image).
You can't use SQLite on Heroku. Its filesystem is dyno-local and ephemeral. Any changes you make to it will be lost whenever your dyno restarts. This happens frequently (at least once per day).
Heroku offers good support for client-server databases. If you want to use Heroku you'll have to switch to one. Their own PostgreSQL service works well and is available out of the box. If you prefer other databases feel free to browse the data store addons.

What GIT / workflow do I use to avoid overwriting my server side Django users, with my local users who are dummy/test users?

I am developing a Django application in Atom locally, and then on pythonanywhere, when I'm ready, I'm doing a GIT PUSH command after syncing those changes to GITHUB. The problem is, at some point, those changes which were pushed through from my local development env have overwritten all my live users with just the local dummy users I've been using for testing and development.
Basically, I'm testing the login and registration system locally, logging in and registering with lots of dumb emails. Once I was happy it was working, I synced the Django code I'd changed to GITHUB (with the desktop app) and then did a GIT PUSH command on a PythonAnywhere (my server) console. The sqlite DB is included in those updates/sync - is that correct? Or should it just be totally ignored?
I just realised, that one (perhaps all?) pushes have overwritten my sqlite DB, and there were perhaps 30 or so actual users who had signed up on the website whose data is no longer registered on the site. I managed to find a trace of them in the Django Admin logs, and I've found the version history of the Sqlite DB on GITHUB, but my question is - how do I avoid this happening?
What is the workflow to avoid this situation in the future? And is there a command I can run in shell to get those users back into my 'live' database from the backedup SQlite file?
I know this is a simple question, but I'm new to development and I'm slowly getting there with troubleshooting the code, but versioning, GIT, and workflow are tricky things to get my head around.

using mysql instead of sqlite3

im working on python application that requiring database connections..I had developed my application with sqlite3 but it start showing the error(the database is locked).. so I decided to use MySQL database instead.. and it is pretty good with no error..
the only one problem is that I need to ask every user using my application to install MySQL server on his pc (appserv for example) ..
so can I make mysql to be like sqlite3 apart of python lib. so I can produce a python script can be converted into exe file by the tool pyInstaller.exe and no need to install mysql server by users???
update:
after reviewing the code I found opened connection not closed correctly and work fine with sqllite3 ..thank you every body
It depends (more "depends" in the answer).
If you need to share the data between the users of your application - you need a mysql database server somewhere setup, your application would need to have an access to it. And, the performance can really depend on the network - depends on how heavily would the application use the database. The application itself would only need to know how to "speak" with the database server - python mysql driver, like MySQLdb or pymysql.
If you don't need to share the data between users - then sqlite may be an option. Or may be not - depends on what do you want to store there, what for and what do you need to do with the data.
So, more questions than answers, probably it was more suitable for a comment. At least, think about what I've said.
Also see:
https://stackoverflow.com/questions/1009438/which-database-should-i-use-for-my-desktop-application
Python Desktop Application Database
Python Framework for Desktop Database Application
Hope that helps.
If your application is a stand-alone system such that each user maintains their own private database then you have no alternative to install MySQL on each system that is running the application. You cannot bundle MySQL into your application such that it does not require a separate installation.
There is an embedded version of MySQL that you can build into your application (thanks, Carsten, in the comments, for pointing this out). More information is here: http://mysql-python.blogspot.com/. It may take some effort to get this working (on Windows you apparently need to build it from source code) and will take some more work to get it packaged up when you generate your executable, but this might be a MySQL solution for you.
I've just finished updating a web application using SQLite which had begun reporting Database is locked errors as the usage scaled up. By rewriting the database code with care I was able to produce a system that can handle moderate to heavy usage (in the context of a 15 person company) reliably still using SQLite -- you have to be careful to keep your connections around for the minimum time necessary and always call .close() on them. If your application is really single-user you should have no problem supporting it using SQLite -- and that's doubly true if it's single-threaded.

Problem with django nose and south having multiple databases

I had a django project with one database (default). South was installed for generating migration scripts and nose as a test framework. Models were built on this database. All tests were run successfully.
Subsequently, I needed to connect a second database (legacy), which I also added to the DATABASES configuration. I access this database using raw sql and no models. While trying to run the previously running tests, I noticed that:
nose also creates a test database for the legacy database
default django tables (auth_... etc) are also created in this database
south runs all migration scripts against the legacy database as well and fail to do so
What I would like is to disable the creation of the test legacy database and the running of the migration scripts on it. Ideally, I would like to create tables in the test legacy database myself by issuing raw sql create-insert statements. Is that possible?
Thank you for any help.
Your path of least resistance is probably to write your own test running management command. To do so you can either override the existing command or simply create a separate command with your desired behavior.
The docs for creating custom management commands can be found on the official django docs and you can find a decent example of overriding the stock "test" command in the django-test-extensions project.

Categories

Resources