Sqlite3 - BLOB上的外键不匹配

时间:2015-09-30 12:08:37

标签: sqlite foreign-keys

我正在使用外键,不幸的是,在这种情况下,我无法解决不匹配问题...我错过了什么? tebles由以下人员创建:

CREATE TABLE IF NOT EXISTS `devices` (
    `guid`  BLOB NOT NULL,
    `device_name`   TEXT NOT NULL,
    `class` INTEGER NOT NULL
);

CREATE TABLE IF NOT EXISTS `device_states` (
    `guid_d`    BLOB NOT NULL,
    `id_d`  TEXT DEFAULT NULL,
    `tp`    INTEGER NOT NULL,
    `state` INTEGER NOT NULL,
    `note`  TEXT DEFAULT NULL,
    `status`    INTEGER NOT NULL DEFAULT 0,
    PRIMARY KEY(`guid_d`,`tp`),
    FOREIGN KEY(`guid_d`) REFERENCES devices ( `guid` ) ON DELETE RESTRICT
);
CREATE INDEX IF NOT EXISTS idx_ds_tp ON device_states(`tp`);

INSERT OR IGNORE INTO `devices` VALUES (x'438D6E77DC3946A4819617ADBA18ADB2','guid_door', 1);

选择波纹管按预期工作:

SELECT * FROM `devices` WHERE guid = x'438D6E77DC3946A4819617ADBA18ADB2';
  

返回1行:SELECT * FROM devices WHERE guid = x' 438D6E77DC3946A4819617ADBA18ADB2&#39 ;; (花了1毫秒)

但是,当我尝试将数据插入 devices 表时,我收到错误...

INSERT INTO device_states (guid_d, id_d, tp, state, note, status) 
VALUES (x'438D6E77DC3946A4819617ADBA18ADB2', "D", 132456, 1, "door open", 2);
  

外键不匹配 - " device_states"引用"设备":INSERT   INTO device_states(guid_d,id_d,tp,state,note,status)VALUES   (x' 438D6E77DC3946A4819617ADBA18ADB2'," D",132456,1,"开门",2);

有人看到这个问题吗???非常感谢!

1 个答案:

答案 0 :(得分:1)

documentation说:

  

通常,外键约束的父键是父表的主键。如果它们不是主键,则父键列必须共同受UNIQUE约束或具有UNIQUE索引。

guid没有这样的限制。