【发布时间】:2012-06-19 09:23:35
【问题描述】:
考虑这个简单的模型:
基本位置表:
+-------------------------------+
| Locations |
+-------------------------------+
|(PK) _id Integer Autoincrement |
| name Text(100) Not null |
| is_in_range Integer |
+-------------------------------+
还有更专业的表叫 WifiLocation:
+-------------------------------+
| wifi_location |
+-------------------------------+
| ssid Text(0) Not null |
| signal_threshold Real |
|(PK) _id Integer |
+-------------------------------+
我希望这个模型代表一个继承自 BaseLocation 的 WifiLocation。
所以我在wifi_locations 表的_id 列上添加了一个REFERENCES 子句,如下所示:
CREATE TABLE wifi_locations (_id Integer primary key references Locations(_id), ....)
我正在尝试在这些表之间实现 1:1 的关系。
当我想在wifi_locations 表中插入一行时,我首先在Locations 表中插入适当的值(名称、IsInRange),然后取回rowId。
然后我将其余数据 (ssid) 连同 rowId 作为外键插入到wifi_locations 表中。
所以对 Location 表的插入工作正常,我得到了一个 Id,但是当我尝试使用这个 Id 并将它插入到 wifi_locations 表时,我收到一个 SQL Constraint violation 错误。没有关于究竟出了什么问题的更多细节。
我的架构有什么问题吗?有没有更好的方法来实现这样的建模?
编辑:
确切的错误:
06-16 15:56:42.846: E/Database(2038):
android.database.sqlite.SQLiteConstraintException:
error code 19: constraint failed
【问题讨论】:
标签: android sqlite database-design inheritance schema