SQLiteException 没有这样的 table: main.*_temp
SQLiteException no such table: main.*_temp
我有一个 Xamarin.Forms 应用程序,它在设备本地使用 SQLite 数据库。这是一些示例数据结构:
Table x: id, name
Table y: id, name
Table x_y: id, x_id, y_id
由于 SQLite 不支持更改列,我们在补丁中发送的模式更新之一执行了以下操作:
- 将 table x 重命名为 x_temp
- 创建 new/updated table x
- 将 table x_temp 中的所有数据插入 table x
- 删除 table 如果存在 x
这似乎工作得很好。但是,当我尝试 运行 table x_y 上的插入语句时,出现 SQLite 异常:"no such table: main.x_temp".
当我在调试时查看 SQLite 查询字符串时,根本没有提到 table x_temp。因此,如果我删除整个数据库并重新创建所有内容,插入工作正常。
我有 MSSQL 背景,我是不是对 SQLite 大体上有些了解?来自 table x_y 的外键约束是否试图引用 x_temp 因为我重命名了原来的 table (我可能刚刚回答了我自己的问题)?如果是这样的话,肯定有办法解决这个问题,而不必级联并重新创建每个 table?
如有任何意见,我们将不胜感激。谢谢!
我认为您的问题可能与SQlite版本以及是否开启外键支持有关。
可能性是:-
Is the foreign key constraint from table x_y trying to reference
x_temp because I renamed the original table (I may have just answered
my own question)?
会是问题所在,因为您可能已按照 :-
打开了外键支持
Prior to version 3.26.0 (2018-12-01), FOREIGN KEY references to a table that is renamed were only edited if the PRAGMA foreign_keys=ON, or in other words if foreign key constraints were begin enforced.
With PRAGMA foreign_keys=OFF, FOREIGN KEY constraints would not be changed when the table that the foreign key referred to (the "parent table") was renamed.
Beginning with version 3.26.0, FOREIGN KEY constraints are always converted when a table is renamed, unless the PRAGMA legacy_alter_table=ON setting is engaged. The following table summaries the difference:
If that's the case, surely there is a way around this without having
to cascade and re-create every table?
是的,因为 Android 上最新版本的 SQlite 是 3.19.0(我相信),那么您可以使用 foreign_keys pragma 重命名 table.
时
- 注意无法在事务中关闭外键。
见SQL As Understood By SQLite - ALTER TABLE
and PRAGMA foreign_keys = boolean;
我有一个 Xamarin.Forms 应用程序,它在设备本地使用 SQLite 数据库。这是一些示例数据结构:
Table x: id, name
Table y: id, name
Table x_y: id, x_id, y_id
由于 SQLite 不支持更改列,我们在补丁中发送的模式更新之一执行了以下操作:
- 将 table x 重命名为 x_temp
- 创建 new/updated table x
- 将 table x_temp 中的所有数据插入 table x
- 删除 table 如果存在 x
这似乎工作得很好。但是,当我尝试 运行 table x_y 上的插入语句时,出现 SQLite 异常:"no such table: main.x_temp".
当我在调试时查看 SQLite 查询字符串时,根本没有提到 table x_temp。因此,如果我删除整个数据库并重新创建所有内容,插入工作正常。
我有 MSSQL 背景,我是不是对 SQLite 大体上有些了解?来自 table x_y 的外键约束是否试图引用 x_temp 因为我重命名了原来的 table (我可能刚刚回答了我自己的问题)?如果是这样的话,肯定有办法解决这个问题,而不必级联并重新创建每个 table?
如有任何意见,我们将不胜感激。谢谢!
我认为您的问题可能与SQlite版本以及是否开启外键支持有关。
可能性是:-
Is the foreign key constraint from table x_y trying to reference x_temp because I renamed the original table (I may have just answered my own question)?
会是问题所在,因为您可能已按照 :-
打开了外键支持Prior to version 3.26.0 (2018-12-01), FOREIGN KEY references to a table that is renamed were only edited if the PRAGMA foreign_keys=ON, or in other words if foreign key constraints were begin enforced.
With PRAGMA foreign_keys=OFF, FOREIGN KEY constraints would not be changed when the table that the foreign key referred to (the "parent table") was renamed.
Beginning with version 3.26.0, FOREIGN KEY constraints are always converted when a table is renamed, unless the PRAGMA legacy_alter_table=ON setting is engaged. The following table summaries the difference:
If that's the case, surely there is a way around this without having to cascade and re-create every table?
是的,因为 Android 上最新版本的 SQlite 是 3.19.0(我相信),那么您可以使用 foreign_keys pragma 重命名 table.
时- 注意无法在事务中关闭外键。
见SQL As Understood By SQLite - ALTER TABLE
and PRAGMA foreign_keys = boolean;