oneToOne Relations?

224 views
Skip to first unread message

Ravi Mendis

unread,
Nov 8, 2010, 6:56:03 PM11/8/10
to Squeryl
HI Maxime,

Is there a way to model strict one-to-one relations?
E.g:

Movie (with a pk movieID)
PlotSummary (with a pk movieID)

Movie <-> PlotSummary is oneToOne over (movieID)

FYI: this is an example of table-splitting.

Thanks,
Ravi

CISSE Amadou

unread,
Nov 9, 2010, 3:42:41 PM11/9/10
to squ...@googlegroups.com
hi, i tried a similar stuff and here's what i'd do.

class Movie(val id: Long = 0L) extends KeyedEntity[Long] 

i wouldn't use movieID as pk, squeryl uses id of KeyedEntity in many places so i just follow the convention.
Notice that id has default value, that's cuz i want it be autoincremented (that's the default behavior)

class MovieSummary(val id: Long) extends KeyedEntity[Long] 

Then in the schema:

object MySchema extend Schema {
   val movies = table[Movie]
   val movieSummaries = table[MovieSummary]

  on(movieSummaries)(movieSummary => declare(
    movieSummary.id is primaryKey //this turns off auto-increment for movieSummaries
  ))
 
  //also make the primary key of movieSummary a foreign key to movies(id)
  val movieToSummaries = 
    oneToManyRelation(movies, movieSummaries).
      via((m, ms) => m.id === ms.id)

   //More goodies: remove the summary when the movie is deleted
  movieToSummaries.foreignKeyDeclaration.constrainReference(onDelete cascade)
}

However, now u have to provide the id of the movieToSummary whenever u create one.
But thats evident cuz u're just going to give it the id of the movie.

Hope this helps.

Ravi Mendis

unread,
Nov 9, 2010, 9:25:17 PM11/9/10
to Squeryl
Thx...but was hoping for a oneToMany relation ;)

Maxime Lévesque

unread,
Nov 10, 2010, 7:08:27 AM11/10/10
to squ...@googlegroups.com

 You can open an issue  (http://github.com/max-l/Squeryl/issues) if
you think it would be a usefull feature, it seems like a one to one
would be a slight variation of the OneToMany...

 Cheers

Michael Bayne

unread,
Nov 10, 2010, 12:53:51 PM11/10/10
to squ...@googlegroups.com
2010/11/10 Maxime Lévesque <maxime....@gmail.com>:

>  You can open an issue  (http://github.com/max-l/Squeryl/issues) if
> you think it would be a usefull feature, it seems like a one to one
> would be a slight variation of the OneToMany...

With my recent patch, you can use oneToMany without having to
duplicate the id in the many record. I use that to achieve essentially
a oneToOne relationship in some cases (though a real oneToOne modeling
might be useful as well, if just for clarity and to guide new users
who are trying to set up a one to one relationship and don't
necessarily realize that a oneToMany relationship can be used in that
way).

-- m...@samskivert.com

Reply all
Reply to author
Forward
0 new messages