[英]how to change the data type of a field using peewee in SQLite
租约帮我弄清楚。 我为数据库中的表创建了这个类。
class ProfileAdditionalField(peewee.Model):
profile = peewee.ForeignKeyField(RpProfile, on_delete='cascade')
item = peewee.ForeignKeyField(AdditionalField, on_delete='cascade')
is_allowed = peewee.BooleanField(default=True)
class Meta:
database = db
primary_key = peewee.CompositeKey('profile', 'item')
当我尝试修改 RpProfile 表时,ProfileAdditionalField 表中的所有条目都被删除。 我认为问题在于设置“on_delete = cascade”
playhouse_migrate.migrate(
migrator.add_column('RpProfile', 'show_link',
peewee.BooleanField(default=False)),
)
我使用 SQLite,并且 migrator.alter_column_type 命令在其中不起作用。 我什至无法更改设置,以便不再自动删除数据。 如何在不删除 ProfileAdditionalField 表中的数据的情况下向 RpProfile 表添加新字段?
Sqlite 对更改列的支持有限。 这是有据可查的:
https://sqlite.org/lang_altertable.html
在进行任何更改之前,您可以尝试在 sqlite ( pragma foreign_keys=0
) 中禁用外键约束。 除了简单地在 sqlite 中添加新列之外,为了做任何事情,peewee 必须创建一个具有所需架构的临时表,迁移所有内容,然后删除旧表并重命名临时表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.