我的模型如下所示:
TestGroup
TestPerson
firstName Text
lastName Text
testGroupId TestGroupId
TestObject
objectName Text
testGroupId TestGroupId
在这种情况下,TestGroup表中唯一的东西是testGroupId。多个TestPerson可以在一个组中(一个到多个),一个组可以有多个测试对象(也有一个到多个)。
以下代码编译并运行但产生SQLite错误:
postAddTestPersonR :: Handler Value
postAddTestPersonR = do
newTestPerson <- parseJsonBody :: Handler (Result TestPerson)
case newTestPerson of
Success s -> runDB $ do
newTestGroup <- insert $ TestGroup
_ <- insert $ TestPerson (firstName s) (lastName s) newTestGroup
return $ object ["message" .= "it worked"]
Error e -> return $ object ["message" .= e]
错误:
"INSERT INTO \\\"test_group\\\"() VALUES()\": near \")\": syntax error)"
如果我打开数据库并以这种方式手动添加它可以工作,我会得到一个新的ID号:
INSERT INTO test_group VALUES (null);
我是否应该尝试在Raw SQL中执行此操作,或者通过persist解决此问题。一个简单的解决方案就是向TestGroup添加一个虚拟的变量并执行insert $ TestGroup Nothing
,但这有点过时,我想知道是否有办法解决它。