2013-10-29 47 views
0

要使用db side autoinc id大多數事情建議使用不帶id的自定義(forInsert)投影,現在我想使用相同的投影進行更新,但我無法弄清楚如何(或者如果可能的話)我可以使用映射投影進行更新嗎?

class Users extends Table[User]("user") { 
    def id = column[UserId]("id", O.PrimaryKey, O.AutoInc) 
    def email = column[String]("email") 
    def password = column[String]("password") 

    def * = id.? ~ email ~ password <>(User, User.unapply _) 

    def forInsert = email ~ password <>({ 
    (email, password) => User(None, email, password) 
    }, { 
    u: User => Some((u.email, u.password)) 
    }) 

    def uniqueEmail = index("idx_email", email, unique = true) 
} 

這允許你做的做

Users.forInsert.insert(User(None, "foo", "bar")) 

現在給出一個ID和一個用戶我可以不必設置用戶ID更新行?

查詢(用戶).filter(_。ID == ID).magic(Users.forInsert).update(用戶(無, 「富」, 「酒吧」))

+0

我沒有找到官方文檔中的任何實例關於使用映射投影進行更新 –

回答

0

您不能重複使用刀片投影,但您可以輕鬆定義更新投影並執行。

Users.filter(_。ID === ID).MAP(P => p.email〜p.password).update(電子郵件,密碼)