【发布时间】:2017-04-19 15:12:05
【问题描述】:
我想为每个已经存在的用户填写空的 notification_settings。 ID (PK) 由 Hibernate 在每个表中自动生成。这是用户表:
CREATE TABLE lottery_user (
id int8 not null,
email varchar(255) not null,
password varchar(255) not null,
notification_settings_id int8,
role varchar (50) default 'USER',
registry_date TIMESTAMP default now(),
primary key (id)
ADD CONSTRAINT FK_USER_TO_NOTIFICATION_SETTINGS
FOREIGN KEY (notification_settings_id) REFERENCES notification_settings
);
这是我需要为没有为他们填写的用户填写的 notification_settings 表。
CREATE TABLE notification_settings (
id int8 not NULL ,
test1_events bool DEFAULT TRUE ,
test2_events bool DEFAULT TRUE ,
test3_events bool DEFAULT TRUE ,
test4_events bool DEFAULT TRUE ,
PRIMARY KEY (id)
);
基本上,我需要使用"INSERT INTO notification_settings (test1_events, test2_events, test3_events, test4_events) VALUES (True, True, True, True)" 类似的东西。当然,条件应该是这样的“这些行对用户来说是空的”。我似乎无法正确使用语法。
重要说明: SQL 代码用于演示目的,因此您可以知道我有什么样的表。我只需要正确插入脚本。表工作正常,只需要为已经存在的用户生成 notification_settings 值。
另一个注意事项:使用 Flyway,因此它不仅仅是关于 Hibernate。如果这与任何事情有关。
【问题讨论】:
-
当您将
DEFAULT值添加到表notification_settings时,它们将始终为TRUE。只需插入id。 -
ID 是由 Hibernate 自动生成的,我该怎么做?
-
您能解释一下您的数据模型吗?在当前模型中,
user链接到notification_settings,您只需将notification_settings_id int8的默认值设置为包含true, true, true, true的(一个)id。虽然我不完全确定这就是你要找的。您可能想使用notification_settings中的用户标识而不是新标识。然后你可以在你的user-table 上使用insert-trigger 来做到这一点。您也可以将 4 列添加到user表本身,而根本没有第二个表。 -
不,我不需要在
notification_settings中使用userId FK。我对问题进行了一些编辑,对于我造成的任何混乱,我深表歉意。我只需要为已经存在但没有填写的用户填写第二个表。我已经得到了几个可能有帮助的答案。感谢您的关注。
标签: sql database postgresql hibernate