Scala 处理外键
我找到了一种定义主键的方法:Scala 处理外键,scala,playframework,postgresql-9.2,playframework-2.2,anorm,Scala,Playframework,Postgresql 9.2,Playframework 2.2,Anorm,我找到了一种定义主键的方法: case class User(id: Pk[Long], name: String) 但是我没有找到处理外键的方法。是否有一个或我是否必须将其用作正常字段?您可以将其视为正常字段 顺便说一句,你可以查看示例播放!框架应用程序。你可以在剧中找到他们!在“样本”文件夹中分发。例如,检查计算机数据库项目。数据库中有外键,但在代码中它们被视为普通字段 演变: create table company ( id bigin
case class User(id: Pk[Long], name: String)
但是我没有找到处理外键的方法。是否有一个或我是否必须将其用作正常字段?您可以将其视为正常字段 顺便说一句,你可以查看示例播放!框架应用程序。你可以在剧中找到他们!在“样本”文件夹中分发。例如,检查计算机数据库项目。数据库中有外键,但在代码中它们被视为普通字段 演变:
create table company (
id bigint not null,
name varchar(255) not null,
constraint pk_company primary key (id))
;
create table computer (
id bigint not null,
name varchar(255) not null,
introduced timestamp,
discontinued timestamp,
company_id bigint,
constraint pk_computer primary key (id))
;
alter table computer add constraint fk_computer_company_1 foreign key (company_id) references company (id) on delete restrict on update restrict;
代码:
告诉我们您正在使用的数据库库。事实上,你的问题没有提供足够的上下文,任何人都无法准确回答。@Dylan,我使用的是anorm+postgresql。为什么要使用bigint而不是serial或bigserial?我想你可以在company.id和computer.id字段中使用serial/bigserial而不是bigint,但在computer.company\u id字段中不能使用(这会导致错误).为什么使用bigint而不是serial或bigserial?
case class Computer(id: Pk[Long] = NotAssigned, name: String, introduced: Option[Date], discontinued: Option[Date], companyId: Option[Long])