I'm having some difficulty getting my django tests to run properly; I'm using nose, and I started getting an error when the migrations were being applied, that from table 1 a foreign key relation to table 2 failed with the error:
django.db.utils.DatabaseError: relation "table2_column" does not exist
Looking at the way the migrations were being applied it was clear to me that table1 was not created prior to the foreign key relation was applied, so I tried to figure out how to force the dependency, and found the following article:
http://south.aeracode.org/docs/dependencies.html
I then added:
depends_on = (
("app2", "0001_inital"),
)
to my app1/0001_initial.py file.
Unfortunately now I'm getting the following error:
south.exceptions.DependsOnUnknownMigrationMigration 'app1:0001_initial' depends on unknown migration 'app2:0001_inital'.
Any ideas on how to solve this?
I'm not sure if this will solve your problem, but you can add a setting to use syncdb instead of migrations when running tests. Add the following line to your settings.py
SOUTH_TESTS_MIGRATE = False
You have a typo in the name of the migration it's depending on. It should be:
depends_on = (
("app2", "0001_initial"),
)
This dependency system worked for me, after having exactly the same issue you list here, and then finding the dependency system South's docs.
This error is also thrown if there is an error during the import of the target module: If you've got hand-constructed migrations and you're certain the file name matches your depends_on or needed_by, check the referenced file for errors.
Also, setting SOUTH_TESTS_MIGRATE to False won't fix the problem. It just means you won't see the problem until you try to use the migration.
http://south.readthedocs.org/en/latest/settings.html
(That's still useful if you want to speed up your unittests.)
Related
I'm working on a Django Project with Wagtail which uses two databases. The first one is the standard sql lite database for all django models (called db_tool.sqlite3), the other one is also sql lite but for a wagtail integration (called db.sqlite3).
I wanted to migrate to the db_tool.sqlite3 with the following command
python manage.py make migrations
python manage.py migrate --database db_tool
but now I get the following error message regarding wagtail, which I never got before.
django.db.utils.IntegrityError: NOT NULL constraint failed: wagtailcore_page.draft_title
First of all: I don't understand this, because I named the db_tool in particular and I wonder, why the wagtail integration raises an error when I try to migrate to db_tool.
Second: I see no particular field at my wagtail-pages called draft_title and I don't have any draft page at the moment.
Third: the error message also relates to a migration file of wagtail that can be found in the side-packages (see below). So maybe this is the root of the error, but I don't understand the correlation to the other error message, because since now it worked fine and I changed nothing exept of some content of my wagtail pages.
File "C:\Users\pubr\.conda\envs\iqps_web\lib\site-packages\wagtail\core\migrations\0001_squashed_0016_change_page_url_path_to_text_field.py", line 23, in initial_data
root = Page.objects.create(
The wagtail version I use here is wagtail 2.15.2 and I haven't updated it since I started the project...
Due to the fact, that my wagtail-database has the name of the default django-database, could it be possible, that I accidentally tried a migration which was ment for the tool_db.sqlite3 without naming it in the migrate-command and caused this error by doing that?
So I would be very grateful if anyone knows, where the error comes from, or at least, what I could try out to fix it...
Kind regards and thank you!
It isn't clear to me if your database is currently broken or not. Hopefully not, but if it is, please take a back up of each before doing anything else.
This does sound like you might have been trying to operate in the wrong database. Do you have DATABASE_ROUTERS configured? I think that might help you prevent code from one app from getting introduced into the wrong database. The example in the Django docs is mostly focused on read replicas but should be adaptable to your situation: https://docs.djangoproject.com/en/4.1/topics/db/multi-db/#an-example
If your databases are in an incorrect state, start by looking at the django_migrations file in each and then carefully pruning the messed up one until you get back to the separation you have been enforcing.
So, in a bit more detail I have a model with a field like: permalink = models.IntegerField(default=0)
I've not actually been using this field - but would now like to.
However, it seems all models on this table, permalink is now 57295730 - on all 2000 models!
In an attempt to debug, I tried completly wiping the DB, running migrate (~100 migrations) - but then creating a instance of the model, I am told permalink violates the not-null constraint though I am definitely passing it a value! I also get a list of the values I am passing it, but am not sure how to know which value/column relates to which field?
I've even tried removing DB, removing migrations, running a new makemigrations - and still get the null violation...
even stranger, it looks like this field has not been touched since the initial migration!
migrations$ egrep permalink *
0001_initial.py: ('permalink', models.IntegerField(default=0)),
migrations$
I'm running (k)ubuntu 14.04, postgres 9.3, python 3.4, django 1.9.4
Though I'd love to know how to fix this - my question is really "What can I do to debug this kind of situation?"
Well Not the answer I want - but a working answer:
do automated testing
use CI! Prevent this problem in the first place
And if you are not doing the above...
use git bisect (or if you cant, manually use git reset --hard HEAD~1 to find the problem!)
in mycase, I was over-riding the save function of the model in a... stupid way!
edit:
in a little more detail, I was setting permalink to be 1 greater than the current biggest value - but in an earlier commit, had removed the + 1
However, I did not notice this error quickly, as it did not happen with data in the DB.
So! the error was actually quite informative - had I been running my tests more often (or using CI) I would have been informed of the error instantly, and saved myself quite a headache!
so, in short: **write tests, run them - automatically **
I am trying to add a new field to a model - normally a simple process. On one model, I get an error (adding the same field to a different model in the same app causes no problems at all).
The field:
mediumlink = models.URLField(max_length=500, null=True)
Although, any field type or name has the same error.
The error, when using makemigrations:
django.db.utils.ProgrammingError: column images_locationimage.mediumlink does not exist
LINE 1: ...."imagelink", "images_locationimage"."thumblink", "images_lo...
^
I'm stumped! Any help would be awesome.
There are certain files that Django has to have in a working state before it can do other things -- particularly, I think, where models and views are defined (not sure exactly and the list is much shorter with Django 1.7). In any case, if you reference your change before the migration is made and applied, you can find yourself in a catch-22 of needing the migration before you can make the migration. Always get your database in order first before you start using your changes. (In your case, commenting out the offending code will let you proceed with the migration, then quickly get back to where you were.)
I am changing from a profile model setup, to extending the User model. I am trying to get my South migrations set up according to this guide. I have a few other models that reference the User model, and so when I auto-generate my schema migration I get lines along the lines of:
db.alter_column(u'app_model', 'user',
self.gf('django.db.models.fields.related.ForeignKey')(to=orm['app.user']))
The problem is these migrations cause South to break when I try and migrate:
FATAL ERROR - The following SQL query failed: INSERT INTO "_south_new_app_model" () SELECT FROM "app_model";
The error was: near ")": syntax error
! Error found during real run of migration! Aborting.
! Since you have a database that does not support running
! schema-altering statements in transactions, we have had
! to leave it in an interim state between migrations.
! You *might* be able to recover with:
! The South developers regret this has happened, and would
! like to gently persuade you to consider a slightly
! easier-to-deal-with DBMS (one that supports DDL transactions)
! NOTE: The error which caused the migration to fail is further up.
Error in migration: app:0018_auto__chg_field_model_user.py
DatabaseError: near ")": syntax error
Note: this is a migration created with ./manage.py schemamigration app --auto. I haven't edited it.
The thing is, do I care? The type of the reference stored in the database is the same, and I've done the step of renaming the table. Everything seems to continue working if I remove the alter_table lines from the migration. Is this a terrible, terrible idea, or is it ok?
Sqlite3 is the problem. As you may see here, sqlite3 support just a limited subset of alter table operations so that is causing South to fail the migration. If you need to do this you'll have to port the test database to another engine which supports alter table. Which I recommend, sometimes migrations can be tricky and you may think it's working but is always better to test it.
If you think is working ok, so go on and just ignore the error, but caution, it may be working now but maybe the migrations didn't succeed and later you'll find strange database related bugs in your development environment.
I am trying Chapter 6 from Django book.
After changing some fields to blank=True, and null=True, we are told to update the table using dbshell.
When I type: python manage.py dbshell, the following error appears:
C:\Users\Rafa\Documents\Python\book>python manage.py dbshell
Error: near: "Documents": syntax error.
I tried installing sqlite3.exe in my book directory and running directly from there. I still get the same error anytime I try to run a command. For example, once on the sqlite3 shell (opening executable file):
sqlite>ALTER TABLE books_book ALTER COLUMN publication_date DROP NOT NULL;
Error: near "ALTER": syntax error.
I know the commands may be wrong, but I can try anything and it will always point the same error and show the first word of the command in the "".
Any help?
SQLite has very limited ALTER TABLE support.
You have to drop and recreate the table.
As the error message suggests, there is a syntax error somewhere. You should check (or post here) the parts which describe Documents class for some syntactic mistakes :)
I realize now that Django does not support database migrations very well. Once you do the initial migration and the original table is set, it is impossible to add or modify fields with Django only. You need a database migration schema like South.