Zag*_*xiy 7 python django postgresql django-models
在我的数据库中我有专栏:
currency = models.CharField(max_length=10, blank=True, null=True)
Run Code Online (Sandbox Code Playgroud)
我想将此列更改CharField为IntegerField.所以在models.py我改变这个:
currency = models.IntegerField(blank=True, null=True)
Run Code Online (Sandbox Code Playgroud)
然后我做了迁移:python manage.py makemigrations和python manage.py migrate.在那个动作之后它会出现错误:
django.db.utils.ProgrammingError: column "currency" cannot be cast automatically to type integer
HINT: Specify a USING expression to perform the conversion.
Run Code Online (Sandbox Code Playgroud)
之后在pgAdmin3控制台中进行了以下更改:
ALTER TABLE my_table ALTER COLUMN currency TYPE integer USING (currency::integer);
Run Code Online (Sandbox Code Playgroud)
但我仍然有这个错误,我试图改变所有,但错误不会消失.我必须做什么来逃避这个错误.谢谢
Dhi*_*aTN 11
我认为django迁移不执行转换,我查看了文档,但我没有发现任何有关列转换的事情.
如果现有数据对您来说不重要,您可以删除该列并创建一个新列
如果要保留数据,则需要为新列指定一个不同的名称,并使用旧列作为临时列来保留转换期间的数据.
重要提示: PostgreSQL是更强类型的最新版本,并为解释在这里,除非它明确地做了一些铸造可能无法在PosgreSQL工作.它需要更具体的类型.所以你必须根据你的价值做出正确的选择:
alter table my_table alter column currency type bigint using currency::bigint
Run Code Online (Sandbox Code Playgroud)
或者可能:
alter table my_table alter column currency type numeric(10,0) using currency::numeric
Run Code Online (Sandbox Code Playgroud)
从某些数据类型更改为其他数据类型时,这是一个 PSQL 问题......我遇到了类似的问题,我做了一些有点hackey但它有效......但只是因为我在列中没有任何重要数据
1) 删除该应用程序的最新迁移 2) 删除/注释掉对象上的“列” 3) 迁移缺少列的应用程序 4) 恢复/取消注释有问题的“列” 5) 再次迁移
这是在不使用 sql 的情况下删除和重新创建实际数据库上的列的很长的路要走……我想我会分享,以防它可能对未来有所帮助
| 归档时间: |
|
| 查看次数: |
5956 次 |
| 最近记录: |