2016-05-25 8 views
0

私は既存のプロジェクトを持っています。私はこれを書いていませんでした。そして、彼らがSlickをどのように実装したかについての著者の選択は、私を幾分混乱させます。ここで Slick 2.1.0とTraitsの外来キーの関係

は、クラスの既存のテーブル/スリックセットです

case class SourcesRow(id: Long, 
         childSourceId: Long, 
         childSourceName: String, 
         parentSourceId: Long, 
         parentSourceName: String) 

trait SourcesTable { this : DbProfile => 

    import profile.simple._ 

    class SourcesRows(tag : Tag) extends Table[SourcesRow](tag, "Sources") { 
    def id = column[Long]("Id", O.PrimaryKey, O.NotNull, O.AutoInc) 
    def childSourceId = column[Long]("ChildSourceId", O.NotNull) 
    def childSourceName = column[String]("ChildSourceName", O.NotNull) 
    def parentSourceId = column[Long]("ParentSourceId", O.NotNull) 
    def parentSourceName = column[String]("ParentSourceName", O.NotNull) 

    def * = (id, childSourceId, childSourceName, parentSourceId, parentSourceName) <> (SourcesRow.tupled, SourcesRow.unapply) 
    } 

    val sources = TableQuery[SourcesRows] 

    object SourcesTable { 
    def listSources()(implicit session: SessionDef) = 
     sources.run 
    } 

} 

...と、私たちは今、私は」そう

class ControlledValuesDb(override val profile: JdbcProfile) extends DbProfile 
    with RestrictionsTable 
    with RestrictionCategoriesTable 
    with SourcesTable 
    with CollectionsTable 
    with SiteDestinationsTable 
    with SupplementalCategoriesTable 
    with ListsTable 
    with ItemsTable { 
... 
} 

のようなデータベースオブジェクトにロードされているそれらのいくつか持っています私はSlick 2.1のドキュメントを見てきましたが、オブジェクトから1つのTableQueryを参照する必要があるように見えますが、どのように関係があるのかこれを達成するには、以下をご覧ください。

case class ItemsRow(id: Long , listId: Long, value: String) 
case class ListsRow(id: Long, name: String) 

trait ListsTable { this: DbProfile => 

    import profile.simple._ 

    class ListsRows(tag: Tag) extends Table[ListsRow](tag, "Lists") { 
    def id = column[Long]("Id", O.PrimaryKey, O.NotNull, O.AutoInc) 
    def name = column[String]("Name", O.NotNull) 

    def * = (id, name) <> (ListsRow.tupled, ListsRow.unapply) 
    } 

    val lists = TableQuery[ListsRows] 

    object ListsTable { 

    } 

} 

trait ItemsTable { this: DbProfile => 

    import profile.simple._ 

    class ItemsRows(tag : Tag) extends Table[ItemsRow](tag, "Items") { 
    def id = column[Long]("Id", O.PrimaryKey, O.NotNull, O.AutoInc) 
    def listId = column[Long]("ListId", O.NotNull) 
    def value = column[String]("Val", O.NotNull) 
    //def list = foreignKey("fk_item_list_id", listId, ???)(_.id) 

    def * = (id, listId, value) <> (ItemsRow.tupled, ItemsRow.unapply) 
    } 

    val items = TableQuery[ItemsRows] 

    object ItemsTable { 

    } 

} 

答えて

0

何がしたいことはあなたが従う

class PostTable(tag: Tag) extends Table[BlogPost](tag, "posts") { 
    def pid = column[Long]("pid", O.PrimaryKey, O.AutoInc) 
    def author = column[String]("author") // fk of user table 

    def userFK = 
    foreignKey("author_fk", author, TableQuery[UserTable])(_.email, ForeignKeyAction.Restrict, ForeignKeyAction.Cascade) 

    def * = (pid, author, title, content, postAt, tags) <> (BlogPost.tupled, BlogPost.unapply) 
} 

class UserTable(tag: Tag) extends Table[User](tag, "users") { 
    def email = column[String]("email", O.PrimaryKey) 
    def * = (email, password, name) <> (User.tupled, User.unapply) 
} 

PostTableでお知らせuserFKとしてモデル化することができます関係

を持つテーブルを持っているのであればFK制約 TableQueryは、あなたができるだけのオブジェクトですデータベースを照会するのに使用する

たとえば、コードにval sources = TableQuery[SourcesRows]がある場合は、

・ホープ、このヘルプを意味

sources.filter(_.pid === 1001L) 

=)

+0

そうでもありません。あなたが私の質問を見たなら、私はそのクラス内のクラスとその特性内のコンパニオンオブジェクトを持っていることがわかります。あなたの例を実装しようとすると、ListsRowsはまだアクセスできません。私は書くことができません: "foreignKey(" fk_item_list_id "、listId、TableQuery [ListsRows])(_。id)"。 – djsumdog

+0

また、あなたの例は単なる選択であるため、かなり無価値です。それは外部キーと結合しません。 – djsumdog

+0

あなたの問題がクラス内の特性を持っているなら、それは私にとっては間違っているようですが、解決策は#演算子を使ってアクセスし、ItemsTree#ItemsRowsでItemsRowsにアクセスできます。 Slick APIのドキュメントにある別の方法です。 – Qingwei

関連する問題