为什么外键不起作用在websql ..? [英] why foreign key doesn't work in websql..?

查看:147
本文介绍了为什么外键不起作用在websql ..?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在我的应用程序中使用外键使用web sql ..我测试它在铬..没有错误..但是当我测试手动插入到表img(包含FK)

i期望失败..但插入仍然是成功的..这是我的代码..请帮助我..

  tx.executeSql( PRAGMA foreign_keys = ON;); 
tx.executeSql(CREATE TABLE IF NOT EXISTS img(ID INTEGER PRIMARY KEY ASC,imgID VARCHAR,image VARCHAR,FOREIGN KEY(imgID)REFERENCES trans(ID)),[]);

我尝试使用FK方法保存图像名称数据并连接到转换表。 。

我的插入代码

  tx.executeSql(insert into img(imgID,image)values('100','23.jpg')); 


解决方案是 INSERT code>在与 CREATE 相同的数据库连接上执行?因为 PRAGMA foreign_keys = ON 只对连接有效。它必须为你打开的每个连接执行。



另外,确保在不在事务中时运行PRAGMA语句,因为没有任何效果。你的以 tx。开头的示例代码表明这是你问题的原因。


$ b

UPDATE
正如 MisterSmith 的评论所指出的,基于Chromium的浏览器的扩展不能那么无论如何都要在事务之外运行SQL语句,所以不可能执行外键。

因此,您必须确保您的外键在您的应用程序代码中保持完整性,或者你可以写触发器强制外键(如果触发器是允许Chromium扩展,即)。

i try to use foreign key in my app using web sql.. i test it in chrome.. no error.. but when i test with manual insert to table img (contain FK)
i expect to fail.. but insert is still succes.. this is my code.. please help me..

tx.executeSql("PRAGMA foreign_keys = ON;");    
tx.executeSql("CREATE TABLE IF NOT EXISTS img (ID INTEGER PRIMARY KEY ASC,imgID VARCHAR, image VARCHAR,  FOREIGN  KEY (imgID) REFERENCES trans (ID) )", []);

i this case i try to use FK method for save my image name data and connect to trans table..

my insert code

tx.executeSql("insert into img (imgID,image) values ('100','23.jpg')");

解决方案

Is the INSERT performed on the same database connection as the CREATE? Because the PRAGMA foreign_keys = ON is only valid for the connection. It has to be executed for each connection you open.

Also, make sure that you run that PRAGMA statement when you’re not in a transaction, since that wouldn’t have any effect. Your example code starting with tx. suggests that is the cause of your problem.

UPDATE As pointed out in the comments by MisterSmith, extensions of Chromium-based browsers can't run SQL statements outside a transaction anyway, so no foreign key enforcement is possible.

So, you’ll have to ensure your foreign keys maintain integrity either in your application code, or you can write triggers to enforce the foreign keys (if triggers are permitted to Chromium extensions, that is).

这篇关于为什么外键不起作用在websql ..?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆