Migration error

243 views
Skip to first unread message

Raitucarp

unread,
Jun 10, 2012, 3:00:49 PM6/10/12
to Django users
'ENGINE': 'django.db.backends.sqlite3'

I have a model such this:

class Ngobrol(models.Model):
title = models.CharField(max_length=200, blank = True)
message = models.TextField('Message')
linkp = models.URLField(max_length=255, blank = True)
meta_data = models.TextField('Meta data', blank=True)
hash_tags = models.CharField(max_length=255, blank=True)
pub_date = models.DateTimeField('Date Posted')

def short_message(self):
return self.message[:50]

def __unicode__(self):
return self.title

And I add some data (about 20 data) to it. Then, I add one column name
aa

aa = models.CharField(max_length = 10, blank = True)

I ran manage.py syncdb, and access to my admin: http://localhost:8000/admin/ngobrol/ngobrol/
I got:

DatabaseError at /admin/ngobrol/ngobrol/
no such column: ngobrol_ngobrol.aa

So, I install south, I got this error:

manage.py migrate ngobrol
Running migrations for ngobrol:
- Migrating forwards to 0001_initial.
> ngobrol:0001_initial
FATAL ERROR - The following SQL query failed: CREATE TABLE
"ngobrol_ngobrol" ("i
d" integer NOT NULL PRIMARY KEY, "title" varchar(200) NOT NULL,
"message" text N
OT NULL, "linkp" varchar(255) NOT NULL, "aa" varchar(10) NOT NULL,
"meta_data" t
ext NOT NULL, "hash_tags" varchar(255) NOT NULL, "pub_date" datetime
NOT NULL);
The error was: table "ngobrol_ngobrol" already exists
! 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: = DROP TABLE
"ngobrol_ngobrol"; []
= DROP TABLE "ngobrol_comment"; []

! 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: ngobrol:0001_initial
Traceback (most recent call last):
File "E:\sasisu\manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "E:\Python27\lib\site-packages\django\core\management
\__init__.py", line
443, in execute_from_command_line
utility.execute()
File "E:\Python27\lib\site-packages\django\core\management
\__init__.py", line
382, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "E:\Python27\lib\site-packages\django\core\management\base.py",
line 196,
in run_from_argv
self.execute(*args, **options.__dict__)
File "E:\Python27\lib\site-packages\django\core\management\base.py",
line 232,
in execute
output = self.handle(*args, **options)
File "E:\Python27\lib\site-packages\south\management\commands
\migrate.py", lin
e 107, in handle
ignore_ghosts = ignore_ghosts,
File "E:\Python27\lib\site-packages\south\migration\__init__.py",
line 219, in
migrate_app
success = migrator.migrate_many(target, workplan, database)
File "E:\Python27\lib\site-packages\south\migration\migrators.py",
line 235, i
n migrate_many
result = migrator.__class__.migrate_many(migrator, target,
migrations, datab
ase)
File "E:\Python27\lib\site-packages\south\migration\migrators.py",
line 310, i
n migrate_many
result = self.migrate(migration, database)
File "E:\Python27\lib\site-packages\south\migration\migrators.py",
line 133, i
n migrate
result = self.run(migration)
File "E:\Python27\lib\site-packages\south\migration\migrators.py",
line 107, i
n run
return self.run_migration(migration)
File "E:\Python27\lib\site-packages\south\migration\migrators.py",
line 81, in
run_migration
migration_function()
File "E:\Python27\lib\site-packages\south\migration\migrators.py",
line 57, in
<lambda>
return (lambda: direction(orm))
File "E:\sasisu\ngobrol\migrations\0001_initial.py", line 20, in
forwards
('pub_date', self.gf('django.db.models.fields.DateTimeField')()),
File "E:\Python27\lib\site-packages\south\db\generic.py", line 44,
in _cache_c
lear
return func(self, table, *args, **opts)
File "E:\Python27\lib\site-packages\south\db\generic.py", line 348,
in create_
table
', '.join([col for col in columns if col]),
File "E:\Python27\lib\site-packages\south\db\generic.py", line 273,
in execute

cursor.execute(sql, params)
File "E:\Python27\lib\site-packages\django\db\backends\util.py",
line 40, in e
xecute
return self.cursor.execute(sql, params)
File "E:\Python27\lib\site-packages\django\db\backends
\sqlite3\base.py", line
337, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.DatabaseError: table "ngobrol_ngobrol" already exists

Please help to resolve this problem. Thanks

Hendrik Speidel

unread,
Jun 10, 2012, 3:09:15 PM6/10/12
to django...@googlegroups.com
Hi Raitucarp,

you can use the convert_to_south command to convert an existing app (with existing database tables) to south.
In your case a

manage.py migrate ngobrol 0001 --fake

followed by a "manage.py migrate ngobrol" should fix your issue (given that you ran south initially, before changing the model).

Reference:

http://south.readthedocs.org/en/latest/convertinganapp.html

Good luck!

hendrik

Raitucarp

unread,
Jun 10, 2012, 3:14:51 PM6/10/12
to django...@googlegroups.com

@henzk

I still get the error, when access admin:

Raitucarp

unread,
Jun 10, 2012, 3:31:08 PM6/10/12
to django...@googlegroups.com
Remember that I using sqlite3.

Hendrik Speidel

unread,
Jun 10, 2012, 3:34:24 PM6/10/12
to django...@googlegroups.com
Hi again,

can you verify that there are two migration scripts in your migrations folder?
if yes, did you run manage.py migrate after the initial fake migration?
if not, i speculate, that you ran south only after you already changed the models to include the aa column.
South needs to know the initial state of your models to calculate the difference in the schema.
If this is the case, you now have multiple options to recover:

Option 1: simply add the aa column to the db manually. New migrations should work fine after that.

Option 2:
    -remove the migration scripts.
    -Drop all the south tables.
    -Remove the aa field in your model.
    -syncdb to create the south tables
    -Use the convert_to_south command
    -add the aa field again
    -do a schemamigration
    -migrate


hendrik

P.S: the database you are using shouldn't matter...
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/m0OH9jRnXWkJ.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Melvyn Sopacua

unread,
Jun 11, 2012, 8:23:58 AM6/11/12
to django...@googlegroups.com
On 10-6-2012 21:00, Raitucarp wrote:
> 'ENGINE': 'django.db.backends.sqlite3'
>
> I have a model such this:
>
> class Ngobrol(models.Model):
> title = models.CharField(max_length=200, blank = True)
> message = models.TextField('Message')
> linkp = models.URLField(max_length=255, blank = True)
> meta_data = models.TextField('Meta data', blank=True)
> hash_tags = models.CharField(max_length=255, blank=True)
> pub_date = models.DateTimeField('Date Posted')
>
> def short_message(self):
> return self.message[:50]
>
> def __unicode__(self):
> return self.title
>
> And I add some data (about 20 data) to it. Then, I add one column name
> aa
>
> aa = models.CharField(max_length = 10, blank = True)
>
> I ran manage.py syncdb

And this is covered in the manual:
https://docs.djangoproject.com/en/1.4/ref/django-admin/#syncdb

See the big fat block "syncdb will not alter existing tables".
The easy solution is to drop the table and then run syncdb. The slightly
more complicated one is to run:

# Create an sql dump of your app models
python manage.py sqlall appname > appname.sql

<alter field in models>

# Rename it to previous
mv appname.sql appname.prev.sql
# Make a new dump with your changes to the model
python manage.py sqlall appname > appname.sql
# View the changes using the standard diff tool
diff -u appname.prev.sql appname.sql
# Get a database shell
djmanage dbshell
<manually apply the changes using your leet SQL skills and the diff as a
guide>

--
Melvyn Sopacua
Reply all
Reply to author
Forward
0 new messages