PrimaryKey over multiple columns

2,479 views
Skip to first unread message

MD

unread,
Sep 28, 2010, 11:31:58 AM9/28/10
to ScalaQuery
Hi,

Is there a way of defining a primaryKey over multiple columns when
defining a table?
For example, if i have two foreignKeys which in combination should
represent my primaryKey, how would i do that?

Greetings,

Marcus



Stefan Zeiger

unread,
Sep 28, 2010, 12:37:57 PM9/28/10
to scala...@googlegroups.com, MD
On 2010-09-28 17:31, MD wrote:
> Is there a way of defining a primaryKey over multiple columns when
> defining a table?

Multi-column primary keys are not yet supported but you can define a
regular index / constraint over multiple columns so you can use them in
a foreign key, e.g. from ForeignKeyTest:

object B extends Table[(Int, Int, String)]("b") {
def f1 = column[Int]("f1")
def f2 = column[Int]("f2")
def s = column[String]("s")
def * = f1 ~ f2 ~ s
def bIdx1 = index("b_idx1", f1 ~ f2, unique = true)
}

("index" is a bit of a misnomer. It usually creates a unique constraint
with the given name and an automatically created backing index. I should
rename it to "constraint".)

-sz

Adam Mackler

unread,
Sep 20, 2012, 2:36:11 AM9/20/12
to scala...@googlegroups.com
Is it still the case that multi-column primary keys are not yet supported?  I notice this thread is a couple years old, so maybe things have changed since then.

Adam

Erik Post

unread,
Oct 8, 2012, 8:00:11 PM10/8/12
to scala...@googlegroups.com
Hi Adam, 

There's an example of this in the Slick docs at http://slick.typesafe.com/doc/0.11.1/lifted-embedding.html#constraints:

  def pk = primaryKey("pk_a", (k1, k2))

Cheers,
Erik

Adam Mackler

unread,
Oct 11, 2012, 8:53:16 PM10/11/12
to scala...@googlegroups.com
Thank you very much, Erik, for the response.  That is helpful.

FYI, if one is reading the slick readme on github here, it says to see the wiki for more information, with a link to this page, but no reference (unless I'm missing it) to the page you indicated, and (again, unless I'm missing it) the wiki does not seem to have the information that you provided about compound primary keys.

It would probably be beneficial to people trying to learn to use SLICK if the wiki on github either had the same information as slick.typesafe.com or a reference to it so people don't miss the documentation that is on the latter but missing from the former.

Thanks again,
Adam

Erik Post

unread,
Oct 12, 2012, 6:56:59 AM10/12/12
to scala...@googlegroups.com
Hi Adam,

I'm glad it helped. I submitted a pull request to have the link in Slick's README: https://github.com/slick/slick/pull/49.

Cheers,
Erik
Reply all
Reply to author
Forward
0 new messages