django south is changing my boolean data on init - python

To output my database to json file I would usually do
python manage.py dumptdata --indent=4 > mydata.json
However upon executing the following two commands to setup south:
python manage.py schemamigration myproj --initial
python manage.py migrate myproj --fake
I noticed that two of my booleans in mytable for an entry were switched from FALSE to TRUE! I see that from my GUI Web Interface interacting with the database however to more closely compare what changed and got corrupted I'd like to compare json to json but with south enabled I can no longer use the above command as it tells me
Not synced (use migrations):
- myproj
My table that had entries affected is below, I could have more affected data that I have not uncovered.
class MyConfig(models.Model):
name = models.CharField(max_length=64)
myConfigName = models.CharField(max_length=64, unique=True)
myA = models.ForeignKey(MyA)
myB = models.ForeignKey(MyB)
myBoolA = models.BooleanField()
myBoolB = models.BooleanField()
myBoolC = models.BooleanField()
class Meta:
unique_together = ('name', 'myA', 'myB')
def __unicode__(self):
return '%s_%s_%s' % (self.myA.name, self.myB.name, self.name)

schemamigration and migrate --fake don't modify the database. Do you have any initial_data fixture that could be reloaded when migrating? See https://docs.djangoproject.com/en/1.3/howto/initial-data/
Try to migrate with:
python manage.py migrate --no-initial-data
see south doc for more info about options

I don't think that either an --initial or a --fake should alter the database at all, so I'm surprised that it would modify data. In terms of why you're getting the "Not synced (use migrations)" error, I think it's likely because you faked the initial migration.
Try un-migrating the --fake and re-applying the initial migration with
python manage.py migrate --fake zero
python manage.py migrate
Then, you should be able to do the dumptdata

Related

python manage.py migrate relation "" does not exist

I have 2 models - Facility and Slot.
class Facility(BaseModel):
name = models.CharField()...
class Slot(BaseModel):
name = models.CharField()...
slot = models.ForeignKey(Facility)
These work perfectly fine.
Now I am adding another model:-
class BlockedSlot(BaseModel):
slots = models.ManyToManyField(Slot)
Now when I run python manage.py makemigrations, it successfully creates a migration file.
But when I run python manage.py migrate, it gives the error - relation "facilities_slot" does not exist.
(models are in facilities.py)
I even tried resetting the db and trying, but it still fails.
I even deleted all the existing migrations and reran makemigrations that generated the 0001_initial.py and again ran migrate, but still fails.

Django 1.8: Delete/rename a model field in data migration

I need to change the relationship of a model field from ForeignKey to ManyToManyField. This comes with a data migration, to update the pre-existing data.
The following is the original model (models.py):
class DealBase(models.Model):
[...]
categoria = models.ForeignKey('Categoria')
[...]
)
I need the model field 'categoria' to establish a many2many relationship with the model 'Categoria' in the app 'deal'.
What I did:
Create a new field 'categoria_tmp' in DealBase
class DealBase(models.Model):
categoria = models.ForeignKey('Categoria')
categoria_tmp = models.ManyToManyField('Categoria',related_name='categoria-temp')
make a schema migration
python manage.py makemigrations
Edit the migrationfile.py to migrate data from categoria to categoria-tmp
def copy_deal_to_dealtmp(apps, schema_editor):
DealBase = apps.get_model('deal', 'DealBase')
for deal in DealBase.objects.all():
deal.categoria_tmp.add(deal.categoria)
deal.save()
class Migration(migrations.Migration):
dependencies = [
('deal', '0017_dealbase_indirizzo'),
]
operations = [
migrations.AddField(
model_name='dealbase',
name='categoria_tmp',
field=models.ManyToManyField(related_name='categoria-temp', to='deal.Categoria'),
preserve_default=True,
),
migrations.RunPython(
copy_deal_to_dealtmp
)
]
make data migration
python manage.py migrate
Finally I need to delete the column 'dealbase.categoria' and rename the column 'dealbase.categoria-tmp' to 'dealbase.categoria'
I'm stuck at step 5.
Could someone help me out? I cannot find online an answer, I'm using Django 1.8.
Thanks!
You just need to create two additional migrations: one to remove the old field and the other to alter the new field.
First remove dealbase.categoria and create a migration and then rename dealbase.categoria-tmp to dealbase.categoria and create another migration.
This will delete the first field and then alter the tmp field to the correct name.
Try this, may help you.
Step 1 as yours
python manage.py makemigrations && python manage.py migrate
Open shell
for i in DealBase.objects.all()
i.categoria_tmp.add(i.categoria)
Remove your field categoria
python manage.py makemigrations && python manage.py migrate
Add field
categoria = models.ManyToManyField('Categoria',related_name='categoria-temp')
Then
python manage.py makemigrations && python manage.py migrate
Open shell
for i in DealBase.objects.all():
for j in i.categoria_tmp.all():
i.categoria.add(j)
Remove field categoria_tmp
python manage.py makemigrations && python manage.py migrate
If you don't have any data in this model, just comment on that model, and then run manage.py makemigrations and migrate. Then delete the wrong field and delete the comment code, and make makemigrations and migrate. This also works in Django 2.

Model Changes Django, Mezzanine, South

My model was
class Author(Page):
dob = models.DateField("Date of birth")
i removed dob field and updated model with:
class Author(Page):
name = models.CharField(max_length = 250)
email = models.EmailField()
Then entered two commands:
python manage.py schemamigration project_name 001_initial--add-field Author.name, Author.email
then this command
python manage.py migrate project_name
you can see attach image : these above commands don't allow me to save changes in models.
Need your assistance!
SOLUTION:
1)Before adding your new field go to command prompt and move to directory where your project or application is . Now write this command before adding fields in models.py file python manage.py schemamigration your_project_or_app_name --initial
2)Now write this command python manage.py migrate your_project_or_app_name --fake 0001 (or whichever migration number it returned - 0001 is you migration number) to set the south database to that state (tables already created).
3)Now go to your models.py file an add your new fields in your models.py file, then in your cmd run this command python manage schemamigration your_project_or_app_name --auto
4)Last step to save changes run this last migrate command python manage.py migrate your_project_or_app_name
Solution Reference :Django & South: Adding new field but DatabaseError occurs "table already exists" by Yuji 'Tomita' Tomita

Django, South, and Guardian: Migrate

I have two users, mh00h1 and mh00h2. I also have modelA that defines the following in my models.py:
class Meta:
permissions = (
('read','read'),
('write','write'),
)
From a shell, I went to set permissions:
>>>> frhde = create modelA instance
>>>> assign_perm('read', mh00h2, frhde)
DoesNotExist: Permission matching query does not exist. Lookup parameters were {'codename': u'read', 'content_type': <ContentType: modelA>}
I realized that South didn't migrate my models after I added class Meta to modelA and confirmed this. Changing read to read2 shows that South does not detect the change.
$ /manage.py schemamigration myapp --auto
Running migrations for myapp:
- Nothing to migrate.
- Loading initial data for myapp.
Installed 0 object(s) from 0 fixture(s)
Running migrations for guardian:
- Nothing to migrate.
- Loading initial data for guardian.
Installed 0 object(s) from 0 fixture(s)
How can I get schematicmigration to correctly update the database, or is there a better way to do this that does not require redoing the whole database?
Thank you.
You can use ./manage.py syncdb --all, or create a signal like in this post.

How to fix a Database Error and ghost migration error in Django?

I am getting an DatabaseError saying no column named playlist exists and I'm trying to figure out how to fix it. I'm using South. I deleted the old files in the my migrations folder and ran:
python manage.py schemamigration app_name --initial
python manage.py migrate reserve
I get this error when I do that:
south.exceptions.GhostMigrations:
! These migrations are in the database but not on disk:
<reserve: 0002_initial>
! I'm not trusting myself; either fix this yourself by fiddling
! with the south_migrationhistory table, or pass --delete-ghost-migrations
! to South to have it delete ALL of these records (this may not be good).
I'm not sure how to get rid of this error, since in my migrations folder I only have init.py(c) and 0001_initial.py(c); I don't have 0002 migration file anymore.
When I try runserver and click "add playlist" in the admin, this is when I get the DatabaseError. If it helps, my models.py is:
class UserProfile(models.Model):
user = models.OneToOneField(User)
def __unicode__(self):
return self.user
def create_user_profile(sender, instance, created, **kwargs):
if created:
UserProfile.objects.create(user=instance)
post_save.connect(create_user_profile, sender=User)
class Playlist(models.Model):
playlist = models.CharField('Playlist', max_length = 2000, null=True, blank=True)
def __unicode__(self):
return self.playlist
class Video(models.Model):
video_url = models.URLField('Link to video', max_length = 200, null=True, blank=True)
def __unicode__(self):
return self.video_url
class UserPlaylist(models.Model):
profile = models.ForeignKey(User)
playlist = models.ForeignKey(Playlist)
def __unicode__(self):
return self.playlist
class Videoplaylist(models.Model):
video = models.ForeignKey(Video)
playlist = models.ForeignKey(UserPlaylist)
def __unicode__(self):
return self.playlist
Any advice on how to fix this?
Just run
python manage.py migrate reserve --delete-ghost-migrations
This should remove non existing migration from the database table south_migrationhistory.
First, you should work out what happened to get the db and filesystem out of sync.
Then, if appropriate, you can do
python manage.py migrate reserve --ignore-ghost-migrations
or
python manage.py migrate reserve --delete-ghost-migrations
as Aidas said, whichever seems more appropriate. The ignore option is probably less risky, although something has already gone astray for you to get to this state.
South stores migration information in the database too, in a table called "migrations". [ I think thats the table name; writing this from memory ].
You need to clear that table out.
Note
Once you clear that table out, you have to start the migrations again from scratch; right from the initial migration.
It would be a good idea to make a copy of your database as-is before you do this. I assume that your code is already version controlled.
Usually this error happens because, you created a migration file and did the migration then, that migration file was deleted from your file system(disk)
So you have changes in your database caused by a migration that no longer exists.
Depending on whether you deleted the migration file file by choice, what you can do; is go ahead and also delete the changes from the database.
Start the python shell;
$ python manage.py shell
>>from south.models import MigrationHistory
>>MigrationHistory.objects.filter(migration='0002_initial').delete()
That will have deleted the 0002 migration from the db.
You can now go ahead and create/recreate the migration you want.
Goodluck,
Komu.
Just run the command where manage.py file is present in your directory
./manage.py migrate appname --delete-ghost-migrations

Categories

Resources