Slick 3 中的更新语句不正确。1.x
Incorrect update statement in Slick 3.1.x
我有以下 Slick 操作更新 2016 年的所有日期,周六和周日除外
val action = dates.filter( d => d.year === 2016 &&
d.weekDay != "Sat" && d.weekDay != "Sun").map(x => (x.isHoliday)).update(0)
Await.result(db.run(action), Duration.Inf).value
val sql = action.statements.head
println(sql)
问题是更新没有按预期工作,sql 打印
update `dates` set `is_holiday` = ? where ((`dates`.`year` = 2016) and true) and true
何时打印
update `dates` set `is_holiday` = ? where `dates`.`year` = 2016
and `dates`.`week_day` != 'Sat' and `dates`.`week_day` != 'Sun'
这个说法有什么问题?
我相信您不小心使用了 scala !=
运算符而不是 Slick =!=
函数。试试这个:
val action = dates.filter(d => d.year === 2016 && d.weekDay =!= "Sat" && d.weekDay =!= "Sun").map(x => (x.isHoliday)).update(0)
你也可以试试这个:
val weekendDays = Set("Sat", "Sun")
val action = dates.filter(d => d.year === 2016 && !d.weekDay.inSet(weekendDays))
我有以下 Slick 操作更新 2016 年的所有日期,周六和周日除外
val action = dates.filter( d => d.year === 2016 &&
d.weekDay != "Sat" && d.weekDay != "Sun").map(x => (x.isHoliday)).update(0)
Await.result(db.run(action), Duration.Inf).value
val sql = action.statements.head
println(sql)
问题是更新没有按预期工作,sql 打印
update `dates` set `is_holiday` = ? where ((`dates`.`year` = 2016) and true) and true
何时打印
update `dates` set `is_holiday` = ? where `dates`.`year` = 2016
and `dates`.`week_day` != 'Sat' and `dates`.`week_day` != 'Sun'
这个说法有什么问题?
我相信您不小心使用了 scala !=
运算符而不是 Slick =!=
函数。试试这个:
val action = dates.filter(d => d.year === 2016 && d.weekDay =!= "Sat" && d.weekDay =!= "Sun").map(x => (x.isHoliday)).update(0)
你也可以试试这个:
val weekendDays = Set("Sat", "Sun")
val action = dates.filter(d => d.year === 2016 && !d.weekDay.inSet(weekendDays))