Django: Convert CharField to TextField with data intact

后端 未结 3 2005
醉酒成梦
醉酒成梦 2021-02-07 15:32

Is there a way to change a CharField to a TextField and keep the data from this column intact?

Right now I have the following:

class TestLog(models.Mod         


        
3条回答
  •  野的像风
    2021-02-07 15:51

    Assuming that you have access to the database, as you mentioned the way Django talks about adding and removing columns, I've listed methods for both Postgresql and MySQL since you didn't mention what you were using.

    Postgresql:
    BEGIN;
        ALTER TABLE "TestLog"
            ALTER COLUMN "failed_reqs" TYPE TEXT,
            ALTER COLUMN "passed_reqs" TYPE TEXT;
    COMMIT;
    
    MySQL:
    BEGIN;
        ALTER TABLE TestLog
            MODIFY failed_reqs TEXT NULL,
            MODIFY passed_reqs TEXT NULL;
    COMMIT;
    

    I would highly recommend backing up your database before doing these changes.

提交回复
热议问题