migration varchar to date

22 views
Skip to first unread message

Carlos Rocha

unread,
Aug 4, 2014, 5:03:25 PM8/4/14
to south...@googlegroups.com
Hi, i have database legacy and my table have 1 column name "date" is varchar all data corrrect store like that 2013-01-01
but a would like convert this varchar to date

date=models.CharField(max_length, null=True, blank=True)

chage this

date = models.DateField(null=True, blank=True)

when i apply the schema migration all ok

python manage.py schemamigration appname --auto

OK

but when apply migration this error:
django.db.utils.ProgrammingError: column "date" can not be cast automatically to the type date 
HINT: Specify a USING expression to perform the conversion.

Cheers


Andrew Godwin

unread,
Aug 4, 2014, 5:33:28 PM8/4/14
to south...@googlegroups.com
As the error message suggests, you can't just alter CharFields into DateFields, as you can't guarantee that you can do that conversion for every value (try converting "i242jn242jh2" into a date).

The right thing to do here is to write your own ALTER TABLE statement with a cast in it; your database's docs will have more information.

Andrew


--
You received this message because you are subscribed to the Google Groups "South Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to south-users...@googlegroups.com.
To post to this group, send email to south...@googlegroups.com.
Visit this group at http://groups.google.com/group/south-users.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages