Why does referencing a SQLite rowid cause foreign key mismatch?(为什么引用 SQLite rowid 会导致外键不匹配?)
本文介绍了为什么引用 SQLite rowid 会导致外键不匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
SQLite version 3.7.9 2011-11-01 00:52:41
sqlite> PRAGMA foreign_keys = 1;
sqlite> CREATE TABLE foo(name);
sqlite> CREATE TABLE bar(foo_rowid REFERENCES foo(rowid));
sqlite> INSERT INTO foo VALUES('baz');
sqlite> SELECT rowid, name FROM foo;
1|baz
sqlite> INSERT INTO bar (foo_rowid) VALUES (1);
Error: foreign key mismatch
为什么会出现这个错误?这是一个 DML 错误,但我不知道出了什么问题,因为:
Why does this error occur? It is a DML error, but I don't know what's wrong because:
foo存在.foo.rowid存在.foo.rowid是foo的主键,因此被限制为唯一性.bar.foo_rowid是一列,这与foo.rowid是一列的事实相匹配.
fooexists.foo.rowidexists.foo.rowidis the primary key offooand therefore constrained to uniqueness.bar.foo_rowidis one column, which matches the fact thatfoo.rowidis one column.
推荐答案
SQLite 文档对外键非常清楚:
SQLite documentation is quite clear on foreign keys:
The parent key must be a named column or columns in the parent table, not the rowid.
(请参阅此处.)
您不能为此使用 rowid,因此只需为表定义您自己的自动递增主键.
You can't use rowid for this, so just define your own auto incrementing primary key for the table.
这篇关于为什么引用 SQLite rowid 会导致外键不匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
织梦狗教程
本文标题为:为什么引用 SQLite rowid 会导致外键不匹配?
基础教程推荐
猜你喜欢
- 什么是 orradiag_<user>文件夹? 2022-01-01
- mysql选择动态行值作为列名,另一列作为值 2021-01-01
- MySQL 中的类型:BigInt(20) 与 Int(20) 2021-01-01
- 如何根据该 XML 中的值更新 SQL 中的 XML 2021-01-01
- 二进制文件到 SQL 数据库 Apache Camel 2021-01-01
- 如何在 SQL 中将 Float 转换为 Varchar 2021-01-01
- 在 MySQL 中:如何将表名作为存储过程和/或函数参数传递? 2021-01-01
- 在多列上分布任意行 2021-01-01
- 表 './mysql/proc' 被标记为崩溃,应该修复 2022-01-01
- oracle区分大小写的原因? 2021-01-01
