When trying to run my tests (python manage.py test) I am getting:
CommandError: Database test_db couldn't be flushed. Possible reasons:
* The database isn't running or isn't configured correctly.
* At least one of the expected database tables doesn't exist.
* The SQL was invalid.
Hint: Look at the output of 'django-admin.py sqlflush'. That's the SQL this command wasn't able to run.
The full error: cannot truncate a table referenced in a foreign key constraint
DETAIL: Table "install_location_2015_05_13" references "app".
HINT: Truncate table "install_location_2015_05_13" at the same time, or use TRUNCATE ... CASCADE.
I am using partitions in our project which are generated on the fly via a python function (so I can run it periodically). I don't have any models for these partitions.
The partition maintenance function is invoked after syncdb triggers the post_syncdb signal (so it is executed when the test database is set up).
How can I make Django clear the additional tables (partitions)? or
How can I tell Django to use CASCADE while running the tests?
Mainly problem occur where we change relation in M2M field. Old constraint remain and new constraint for new relation was created. Fixed in Django 1.8
Related
So I've made some changes to my schema on a Flask Server, using SQLite and SQLAlchemy. The database is generally functional and I'm able to add some of the models, as well as update and query all models without issues.
I've changed the id in two of my models from Integer to String in order to implement uuid ids, and since I've received IntegrityError for the mismatching parameter types when I do db.session.add(new_post) and db.session.commit().
If I do flask db migrate, it reports that no changes have been detected. Should I manually fill out a revision file or is there something else I am missing?
In addition to migrating your database, you'll need to upgrade:
flask db upgrade
If this results in an error, you might be encountering database specific constraints. For example, some primary keys cannot be removed without setting cascade rules on deletion for relationships.
I have inserted data into table from postgresql directly. Now when I try to insert data from django application, it's generating primary key duplication error. How can I resolve this issue?
Run
python manage.py sqlsequencereset [app_name]
and execute all or just one for the required table SQL statements in the database to reset sequences.
Explanation:
You probably inserted with primary keys already present in it, not letting postgresql to auto-generate ids. This is ok.
This means, internal Postgresql sequence used to get next available id has old value. You need to rest with sequence to start with maximum id present in the table.
Django manage.py has command intended just for that - print sql one can execute in db to reset sequences.
I think problem is not in database. please check your django code probably you use get_or_create
I'm looking to write tests for my application. I would like to work with a clean database for all my tests. For various reasons, I cannot create a separate test database.
What I currently do is run everything in a transaction and never commit to the db. However some tests read from the db, so I'd like to delete all rows at the start of the transaction and start from there.
The problem I am running into is with foreign key constraints. Currently I just go through each table and do
cursor.execute("DELETE FROM %s" % tablename)
which gives me
IntegrityError: (1451, u'Cannot delete or update a parent row: a
foreign key constraint fails (`testing`.`app_adjust_reason`,
CONSTRAINT `app_adjust_reason_ibfk_2` FOREIGN KEY (`adjust_reason_id`)
REFERENCES `adjust_reason` (`id`))')
edit: I would like something generic that could be applied to any database. Otherwise I would specifically drop the constraints
A more general approach is to create a database from scratch before the test run and drop it after, use CREATE DATABASE_NAME and DROP DATABASE_NAME. This way, you are always starting with a clean database state and you would not worry about the foreign key or other constraints.
Note that you would also need to create your table schema and (possibly test data) after you create a database.
As a real world example, this is what Django does when you run your tests. The table schema is recreated by the Django ORM from your models, then the fixtures or/and the schema and data migrations are applied.
I'm using Django 1.6 with PostgreSQL and I want to use a two different Postgres users - one for creating the initial tables (syncdb) and performing migrations, and one for general access to the database in my application. Is there a way of doing this?
From ./manage.py help syncdb:
--database=DATABASE Nominates a database to synchronize. Defaults to the
"default" database.
You can add another database definition in your DATABASES configuration, and run ./manage.py syncdb --database=name_of_database_definition. You might want to create a small wrapper script for running that command, so that you don't have to type out the --database=... parameter by hand every time.
south also supports that option, so you can also use it to specify the database for your migrations.
I tested with Python 2.6 or 2.7 with Django 1.5.1. My database is on MySQL 5.0. I've created the settings but now I can't run "inspectdb" on the database. I get
DatabaseError: (1146, "Table 'db1.tableName' doesn't exist")
This happens on a table which has a foreign key referencing a table in another DB. So it should not be db1 there since tableName lives in db2. I saw references to this bug from 5 years ago:
https://code.djangoproject.com/ticket/7556
But the patch is outdated by now and I figured it must have been finished in a later release. Is it something wrong with my setup?
Unfortunately, Django still does not currently does not support this feature.
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.
However, a fix could be found on this patch here
Basically, updating the router settings self.rel.to