I'm still learning all the ins and outs of web development, scala, play 2.0, and squeryl and I'm trying to set up a ManyToMany relation between two of my tables.

I've looked over the information found here but I'm having trouble with the intermediary table. I've looked all over and I can't find a good example of how it should be structured.

I'm using MySQL for my database and I've tried using foreign keys and primary keys in the intermediary table, but neither have worked, or maybe I'm just doing it wrong. So, could someone give me a clear example of how the intermediate table should look?

For a little more information, the basic structure of the two tables I want to relate are as follows.

tableOne (
  name varchar(255)
);

tableTwo (
  name varchar(255),
  idCode varchar(255)
);

They will be related by the name in tableOne and the idCode in tableTwo which is just an abbreviated form of tableTwo's name column.

So using MySQL, squeryl, and the format shown in the link, can anyone help me get this going?

有帮助吗?

解决方案

To relate the two tables, you will need to establish a ManyToMany relationship in your schema. Assuming you have defined your tables with the names tableOne and tableTwo in your schema, something like this probably what you want:

First create a class that joins the two tables:

class TableOneToTwo(
  name:String = "",
  idCode:String = "") extends KeyedEntity[CompositeKey2[String, String]] {
  def id = compositeKey(name, idCode)
}

Then map the relation in your Schema

val tableOneToTwo = manyToManyRelation(tableOne, tableTwo).via[TableOneToTwo]((t1, t2, jt) => (t1.name === jt.name, t2.idCode === jt.idCode)) 

Then you would just need to create the corresponding table in your DB - which should have two fields - name, id_code (named according to your naming convention).

If you need a reference, this should point you in the right direction: http://squeryl.org/relations.html

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top