如何在 SQLite 中使用 peewee 更改字段的数据类型

how to change the data type of a field using peewee in SQLite

请帮我弄清楚。我为数据库中的 table 创建了这个 class。

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 table 时,ProfileAdditionalField table 中的所有条目都被删除。我认为问题出在设置 "on_delete = cascade"

    playhouse_migrate.migrate(
        migrator.add_column('RpProfile', 'show_link',
                            peewee.BooleanField(default=False)),
    )

我使用 SQLite,migrator.alter_column_type 命令在其中不起作用。我什至无法更改设置以使数据不再自动删除。 如何在不从 ProfileAdditionalField table 中删除数据的情况下向 RpProfile table 添加新字段?

Sqlite 对更改列的支持有限。这是有据可查的:

https://sqlite.org/lang_altertable.html

您可以在进行任何更改之前尝试在 sqlite (pragma foreign_keys=0) 中禁用外键约束。除了简单地在 sqlite 中添加一个新列之外,为了做任何事情,peewee 必须创建一个具有所需模式的临时 table,迁移所有内容,然后删除旧的 table 并重命名临时 [=18] =].