Composite Keys - How?

108 views
Skip to first unread message

bradford

unread,
Feb 2, 2008, 10:16:14 PM2/2/08
to liftweb
I believe someone asked this question about a month ago, but I still
need help with this. How do I do composite keys in lift? I have two
classes: Foo and Bar. Now, I want a class called FooBar that links
the two together. I have two objects extending MappedLongForeignKey:
fooId and barId. How do I make these a composite key of FooBar?

Would something like this work?:

def primaryKeyField = (fooId, barId)

David Pollak

unread,
Feb 2, 2008, 11:04:25 PM2/2/08
to lif...@googlegroups.com
Take a look at MappedPassword for ways to combine multiple database
columns into a a single lift Mapper field.

bradford

unread,
Feb 3, 2008, 2:45:57 PM2/3/08
to liftweb
I'm extremely new to scala and lift (second day using both). I'm
looking to generate something like the SQL code below. Is this what
MappedPassword.scala is doing?

CREATE TABLE FooBar
(
fooId long NOT NULL
barId long NOT NULL
FOREIGN KEY ( fooId ) REFERENCES Foo ( id )
FOREIGN KEY ( barId ) REFERENCES Bar ( id )
PRIMARY KEY ( fooId, barId )
INDEX ( fooId, barId )
);

David Pollak

unread,
Feb 3, 2008, 5:15:43 PM2/3/08
to lif...@googlegroups.com


bradford wrote:
I'm extremely new to scala and lift (second day using both).  I'm
looking to generate something like the SQL code below.  Is this what
MappedPassword.scala is doing?
  
MappedPassword is a single field that represents two columns in the database.

I know you're new to lift and Scala and I'd really like to offer you a nice present by writing the code, but I'm kinda swamped right now and I've got a few other things on my "lift newbies" stack, so I can't commit to working on an example in the next two weeks.  I'm very sorry.

I know Steve Yen was looking into this a few weeks back.  Perhaps he's got an implementation for us.

bradford

unread,
Feb 3, 2008, 5:19:00 PM2/3/08
to liftweb
After reading more about this online, it seems that ORMs have a
difficult time mapping to composite keys. So, if the above is not
possible, would creating FooBar to have an autonumber/serial primary
key with fooId and barId as foreign keys that are unique together work
the same/better? This may be what MappedPasswords is doing??

CREATE TABLE FooBar
(
id serial NOT NULL,
fooId long NOT NULL,
barId long NOT NULL,
FOREIGN KEY ( fooId ) REFERENCES Foo ( id ),
FOREIGN KEY ( barId ) REFERENCES Bar ( id ),
PRIMARY KEY ( id )
);

CREATE UNIQUE INDEX index_name ON FooBar (fooId, barId);

David Pollak

unread,
Feb 3, 2008, 5:56:57 PM2/3/08
to lif...@googlegroups.com


bradford wrote:
After reading more about this online, it seems that ORMs have a
difficult time mapping to composite keys.  So, if the above is not
possible, would creating FooBar to have an autonumber/serial primary
key with fooId and barId as foreign keys that are unique together work
the same/better?  This may be what MappedPasswords is doing??

CREATE TABLE FooBar
(
  id serial NOT NULL,
  fooId long NOT NULL,
  barId long NOT NULL,
  FOREIGN KEY ( fooId ) REFERENCES Foo ( id ),
  FOREIGN KEY ( barId ) REFERENCES Bar ( id ),
  PRIMARY KEY ( id )
  
);

class FooBar extends LongKeyedMapper[FooBar] {
  def getSingleton = FooBar
  def primaryKeyField = id
 
  object id extends MappedLongIndex(this)
  object foo extends MappedLongForeignKey(this, Foo)
  object bar extends MappedLongForeignKey(this, Bar)
}

object FooBar extends FooBar with LogKeyedMetaMapper[FooBar] {
  def dbIndexes = Index(foo, bar) :: super.dbIndexes
}

bradford

unread,
Feb 3, 2008, 5:27:14 PM2/3/08
to liftweb
Yes. Thanks for the hard work that you've put into lift. I will keep
working on this and also wait on Steve Yen.

Thanks
Reply all
Reply to author
Forward
0 new messages