实体中的光滑更新字段在过滤器之后但在更新的 update/exclude 字段之前?
Slick update field in entity after filter but before update/exclude field from update?
在下面的函数中,我正在更新一个用户。但是用户有一个 'createdDate' 字段将在数据库中,但不会在客户端传递的用户实体中:
def update(user: User): Future[Int] = {
db.run(
userTable
.filter(_.userId === user.userId)
// Can I set user.createdDate here to the existing value?
.update(user)
)
}
是否可以在我调用 update(user) 之前使用数据库中现有用户行的值来设置 user.createdDate?
参考这里是我在创建用户时如何设置 createdDate 字段(User 是一个不可变的案例class,所以我做了一个副本来设置 createdDate):
def create(user: User): Future[Int] = {
db.run(userTable += user.copy(
createdDate = Option(Timestamp.valueOf(LocalDateTime.now()))))
}
您可以像这样解构用户对象并跳过 createdDate
。
userTable
.filter(_.id === id)
.map(t => (t.firstName,..., t.modifiedDate))
.update(("John",..., Some(Timestamp.valueOf(LocalDateTime.now))))
我最后只是在更新前读了一遍,像这样:
this.read(user.userId).flatMap {
case Some(existingUser) =>
db.run(
userTable.
filter(_.userId === user.userId).
update(user.copy(createdDate = existingUser.createdDate)))
case None => throw new NotFoundException(...)
}
在下面的函数中,我正在更新一个用户。但是用户有一个 'createdDate' 字段将在数据库中,但不会在客户端传递的用户实体中:
def update(user: User): Future[Int] = {
db.run(
userTable
.filter(_.userId === user.userId)
// Can I set user.createdDate here to the existing value?
.update(user)
)
}
是否可以在我调用 update(user) 之前使用数据库中现有用户行的值来设置 user.createdDate?
参考这里是我在创建用户时如何设置 createdDate 字段(User 是一个不可变的案例class,所以我做了一个副本来设置 createdDate):
def create(user: User): Future[Int] = {
db.run(userTable += user.copy(
createdDate = Option(Timestamp.valueOf(LocalDateTime.now()))))
}
您可以像这样解构用户对象并跳过 createdDate
。
userTable
.filter(_.id === id)
.map(t => (t.firstName,..., t.modifiedDate))
.update(("John",..., Some(Timestamp.valueOf(LocalDateTime.now))))
我最后只是在更新前读了一遍,像这样:
this.read(user.userId).flatMap {
case Some(existingUser) =>
db.run(
userTable.
filter(_.userId === user.userId).
update(user.copy(createdDate = existingUser.createdDate)))
case None => throw new NotFoundException(...)
}