How do I successfully integrate a second database with Django South? - django

One of our clients needs to add some geolocation data to their
site. Since they already have a database setup without GIS extensions,
I decided to create a new database (with the GIS extensions), which I
intend to use to store only the geolocation data.
I had, at some point, set things up to work alright on my development
machine (meaning, I have migrations for these new models). But now that the code has been written, I imported a DB dump
directly from the server so that my development machine exactly
mirrors the production machine, and now I can't seem to get South to
apply the migrations correctly. South seems to have several features
which allow for multiple databases, but none of them have worked so far.
What I've tried:
Just adding the model and migrating. This gives me the following
error:
AttributeError: 'DatabaseOperations' object has no attribute
'geo_db_type'
OK, so South is trying to create the model on the original database
which doesn't have the GIS extensions.
Adding the model, but specifying the 'geo' database for migrating
the 'geo' app. This gives me the following error:
django.db.utils.DatabaseError: relation "south_migrationhistory"
does not exist
I guess south expects its MigrationHistory table to exist on the 'geo'
database as well?
Allow south's models to exist on my 'geo' database.
$ python manage.py syncdb --database=geo
$ python manage.py migrate
This gives me the following error:
django.db.utils.DatabaseError: relation "<model>" already exists
I'm guessing this is because I already have MigrationHistories stored
in the other database?
South apparently has a sparsely documented feature called 'dbs'
(see:
http://south.aeracode.org/docs/databaseapi.html#accessing-the-api )
So I tried the previous three methods again replacing all instances of
"db" with "dbs['geo']".
a. Migrations run smoothly, but don't actually create any tables
in my 'geo' database.
b. Same error as when not using 'dbs' features.
c. Same error as when not using 'dbs' features.
This entire process has been extremely frustrating. Has anyone got
multiple database support up-and-running when using South?

Whenever I have modified the table models I used south and these commands to modify the structure and they always worked:
python manage.py convert_to_south "your_app"
python manage.py migrate "your_app"
I recommend running these commands after running syncdb, so your tables are created.

Related

IntegrityError: Not Null constraint failed

I have deleted a class from model.py but whenever I run python manage.py migrate, I get this:
intergrityerror : not null constraint failed: appname_modelclassfieldname.user_id
The most challenging thing is that I had already deleted the model class of the related field django is pointing error at.
You have to create a migration before running migrate. As you have not specified your Django version, here is what you need to do for the most recent version (1.11) which also works down to at least 1.9:
$ ./manage.py makemigrations # > creates a migration file
$ ./manage.py migrate
You can specify the the app in the makemigrations call if you want.
Concerning:
The most challenging thing is that i had already deleted the model class of the related field django is pointing error at .
Use a version system for your own health (e.g. GIT) or an editor that supports local versioning (like PyCharm) - best use both - even if you are working alone. (You are never alone, after some weeks, the code looks like it was written by someone else...)
The problem was an error with the server .I had delete the app and create a new one ,and everything is working just fine now

Django Programming Error while runing server

I am running a GIS application while getting this error. I have attached error snapshot . Can Someone guide where is error ? If need to see code . Let me know which file you need.
Especially the first line of your exception value pretty much says it all.
Column users.parent_id does not exist
The application is trying to access parent.id from the users table from your database, which obviously does not exist. With other words, your database is not sync with the model structure in the source code. Probably all you have to do, is to run the migrations to add all the missing structures or changes to your database.
If you've developed some of the database structures yourself, you have to run the makemigrations command to build the new migration set for your database.
./manage.py makemigrations
If you have created new migration files or if you have installed and integrated modules into your app, you have to apply the migrations to your database.
./manage.py migrations

django - schema migration - how to add a field

I have a django 1.8 app working with a db.
I'm trying to change the schema of a table using the built-in migration.
Here are the steps I did:
In my dev invironment, I grabbed the app source and ran
python manage.py sycdb
then I ran
python manage.py loaddata ~/my_data.json
then I modified modes.py. Added a field and renamed a field...all from the same table 'TABLE1' which had no data.
then
python manage.py makemigrations myapp
python manage.py migrate
Error: django.db.utils.OperationalError: table "myapp_someother_table" already exists
then ran
python manage.py migrate --fake-initial
worked!
but when I browsed to the admin page for TABLE1, I get this error:
OperationalError: no such column: myapp_table1.my_new_field_id
I checked the db and yes, there is no such column.
How can I procceed from here? I prefer to fix this via django.
If I fix it straight in the db, then the migration goes out of sync.
Migrations do not automagically see that you have made changes. Migrations detect changes by comparing the current model with the historical model saved in the migration files.
In this case, you didn't have any historical models, since you didn't have any migrations. Django was not able to detect any changes in your models, even though they were different from your database.
The correct way to make changes to your model is to first run manage.py makemigration <my_app>, and then make the changes to your model, followed by another manage.py makemigrations.
You might not be able to do it via pure django and keep your data. I don't have personal experience with south but there are a lot of mentions if this tool. Just in case if nothing else works for you...
Here is what I did to make things work, but there must be a better way so please add more answers/comments...
I deleted the sqlite db and the migration folder
I made the desired changes to model.py
ran syncdb
ran loaddata to load the json data dump that I had saved previously.
just started the dev server

newbie difficulty using south with pycharm - DatabaseError: no such table: south_migrationhistory

I'm using sqlite3 and pycharm to learn more about django, and googled to find that south is recommended to make it easier to modify models after they have been created.
I'm trying to follow the advice on http://south.aeracode.org/docs/tutorial/part1.html#starting-off.
The most success I've had so far is to create a simple model and run syncdb before adding south to installed_apps. That way the intial tables are created and I get a chance to create a super user. (Django admin seems to fret if there are no users).
Then I add south to installed_apps, and run django_manage.py schemamigration bookmarks --initial
It seems to work fine. A new directory is created called migrations with a couple of files in it in my app folder and an encouraging message.
"Created 0001_initial.py. You can now apply this migration with: ./manage.py migrate bookmarks"
The next step - django_manage.py" migrate bookmarks generates the following error message
django.db.utils.DatabaseError: no such table: south_migrationhistory.
I thought that table would be created in the first schememigration step. What am I missing? Can anyone help?
Marg
South uses a table if its own to keep track of which migrations have been applied. Before you can apply any migrations, this must have been created, using python ./manage.py syncdb.
As well as for setting up south, you will find syncdb sometimes necessary for non-south apps in your project, such as the very common django.contrib.auth.
Note that as a convenience, you can run both in one go like this
python ./manage.py syncdb --migrate
My latest (unsuccessful) effort was the following
Create application – synch db – superuser created
Test run –admin screen shows basic tables
Add south, and syncdb from command line with manage.py syncdb – south_migrationhistory table created. Add basic vanilla model
Tried various combinations of manage.py syncdb –manage, and
schemamigration from Pycharm (if run from within pycharm a
migrations directory is created within the app
– if run from the command line the directory does not seem to be
created.)
Django admin screen shows table – but if I try to edit
the table it says that it doesn’t exist
Check database structure
using SQLite browser - table for newly created model doesn’t exist
I’m starting to think that the whole thing is not worth the time wasting hassle – maybe I’m better off just modifying the tables in SQLite browser
Answer in the similar question:
Run syncdb to add the Django and South tables to the database.

First steps with PostgreSQL + Django + South

Hi Stackoverflow people,
In the past, I always developed my Django projects locally with sqlite as database platform. Now, I wanted to move to PostgreSQL to take advantage of GIS features, but the transition gives me huge grief.
I have installed postgresql similar to this post and then followed the GeoDjango description for the creation of the database.
Furthermore, I replaced the models class by
from django.contrib.gis.db import models
and add
geolocation = models.PointField(_('Geo Location'),
geography=True,
help_text=_('Geolocation with Longitude and Latitude'))
objects = models.GeoManager()
Now, before diving deeper into the postgreSQL sphere, I wanted to test the model access through the Django Admin, and I encountered the first error.
When I select the model (which i just modified as mentioned above) in the Admin, I get the following error:
**InternalError at /admin/remap/project/**
current transaction is aborted, commands ignored until end of transaction block
This error is connected with a wrong sql query, but I am surprised that the Django Admin creates wrong sql statements (used by million developers and it worked fine in the earlier sqlite config).
When I check the django sql statement, I can see the entry for the PointField
"geolocation" geography(POINT,4326) NOT NULL,
but when I check psql \d projects, I can't see the change to the PointField (this is what should cause the error). Since I am using South, I executed
./manage.py schemamigration projects --initial
./manage.py migrate projects
but I get the message
Running migrations for projects:
- Nothing to migrate.
- Loading initial data for projects.
How can I convince south / postgresql that there is something to migrate?
Do you see any other problem with the transition form SQLite to PostgreSQL?
Thank you for your answer & help!
A possible answer:
Did you syncdb? Often when I'm having trouble with south, I just delete the database and run manage.py syncdb from scratch to make sure all my columns are in line. If you want south to ignore migration-enabled apps and just syncdb up to the current version, you can use manage.py syncdb --all