我正在使用 Play Framework 2.0 (Java),但在全局中实现简单的 InitialData 后,我遇到了一些问题。 我正在使用
我有 2 个模型:
- 用户(电子邮件地址作为 ID)
- SomeModel(长 ID 作为 ID)
SomeModel 有一个字段,例如:
@ManyToOne
public User user;
因此,有一个 User 有许多 SomeModel 连接到它。
在我的初始数据中,我添加了一个用户来测试某些内容:[email protected] .
因此,如果我尝试向该测试用户添加 SomeModel,即使我尝试添加多个模型,也没有问题。
当我在 InitialData 中添加 SomeModel(让我们调用 TestModel)时,我认为没有问题,因为一切都在这个 TestModel 中运行。但刚才,我看到如果我尝试向测试用户添加新的 SomeModel,则会出现以下问题:
[PersistenceException: ERROR executing DML bindLog[] error[Unique index or primary key violation: "PRIMARY_KEY_D ON PUBLIC.SOME_MODEL(ID)"; SQL statement:\n insert into some_model (id, name, description, user_email) values (?,?,?,?) [23505-158]]]
这是我的初始数据文件:
# Users
users:
- !!models.User
email: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bbcfdec8cffbcfdec8cf95d8d4d6" rel="noreferrer noopener nofollow">[email protected]</a>
name: Test test
password: test
# SomeModels
somemodels:
- !!models.FuzzySystem
id: 1
name: test
description: test
user: !!models.User
email: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="661203151226120315124805090b" rel="noreferrer noopener nofollow">[email protected]</a>
我迷失了=(
最佳答案
所以,我刚刚发现initialData不应该有SomeModel的ID...
删除 ID 字段后,一切正常。
这就是我的初始数据文件的样子:
# Users
users:
- !!models.User
email: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b2c6d7c1c6f2c6d7c1c69cd1dddf" rel="noreferrer noopener nofollow">[email protected]</a>
name: Test test
password: test
# SomeModels
somemodels:
- !!models.FuzzySystem
name: test
description: test
user: !!models.User
email: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b2c6d7c1c6f2c6d7c1c69cd1dddf" rel="noreferrer noopener nofollow">[email protected]</a>
关于playframework-2.0 - 尝试在 Globals.InitialData 之后添加模型时出现 PersistenceException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13327485/