2013-01-03 3 views
2

이것은 다른 자동 증가 질문과 비슷한 질문이지만 autoInc을 정의하기 위해 열 목록을 제공하는 대신 어떻게 든 테이블 참조를 제공해야합니다. 나는 다음주는 스칼라 슬릭 자동 증가 필드

import scala.slick.driver.MySQLDriver.simple._ 
import play.Logger 
import slick.session.Database.threadLocalSession 

object PaypalCartItems extends Table[PaypalCartItem]("paypalCartItem") { 
    def id   = column[Int]("id", O.PrimaryKey, O.AutoInc) 
    def itemName = column[String]("item_name") 
    def sku   = column[String]("item_number") 
    def quantity = column[String]("quantity") 
    def txnId  = column[String]("txn_id") 
    def url   = column[String]("url") 

    def * = itemName ~ sku ~ quantity ~ txnId ~ url ~ id <> (PaypalCartItem, PaypalCartItem.unapply _) 

    // I don't want to list out the columns, I need to reference the table 
    def autoInc = itemName ~ sku ~ quantity ~ txnId ~ url returning id 

    def insertItems(items: List[PaypalCartItem]): List[PaypalCartItem] = items map { item: PaypalCartItem => 
    val newId = PaypalCartItems.autoInc.insert(item) 
    item.copy(id=newId) 
    } 
} 

위의 코드는 컴파일되지 않습니다 MySQL의

을 사용하고 있습니다 : 그것은 당신을 도울 것입니다

[error] overloaded method value insert with alternatives: 
[error] [TT](query: scala.slick.lifted.Query[TT,(String, String, String, String, String)])(implicit session: scala.slick.session.Session)Seq[Int] <and> 
[error] (value: (String, String, String, String, String))(implicit session: scala.slick.session.Session)Int 
[error] cannot be applied to (PaypalCartItem) 
[error]  val newId = PaypalCartItems.autoInc.insert(item) 
+0

"어떻게 든 테이블 참조를 제공해야합니다."라는 의미가 확실하지 않습니다. – pedrofurla

답변

0

확실하지,하지만 난 그런 식으로 작업을 수행합니다

def autoInc2 = * returning id.? into { 
     case (Music(_, title, artist, album, isrc, ownerId), id) => Music(id, title, artist, album, isrc, ownerId) 
    } 

을 Music의 첫 번째 인수가 id이면이 경우 Option [Int]입니다. 그리고 그것은 다음과 같이 사용되었습니다 val musicWithId = Musics.autoInc2.insert(someMusicObjectWithId)

+0

이 방법은 효과적이지만 깨지기 쉽고 길다. 사례 클래스의 각 속성은 두 번 열거해야합니다. 사례 클래스의 복사 생성자가 대신 명명 된 매개 변수를 사용하거나 unapply (music) .get을 사용하여 대소 문자 클래스 속성을 튜플로 변환 한 다음 id에 대해 다른 값으로 다시 반환하는 것이 좋습니다. 그래도 많은 소란을 피우는 것처럼 보입니다. –