Incerteza
Incerteza

Reputation: 34884

Deal with foreign keys

I found a way of defining a primary key in Play:

case class User(id: Pk[Long], name: String)

But I didn't find any way to deal with foreign keys. Is there any one or do I have to use it as a normal field?

Upvotes: 2

Views: 305

Answers (1)

psisoyev
psisoyev

Reputation: 2168

You can treat it as a normal field.

By the way you can check sample Play! framework applications. You can find them within the Play! distribution in folder "samples". For example, check computer-database project. There are foreign keys in database, but in the code they are treated like a normal fields.

Evolution:

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;

Code:

case class Computer(id: Pk[Long] = NotAssigned, name: String, introduced: Option[Date], discontinued: Option[Date], companyId: Option[Long])

Upvotes: 1

Related Questions