我如何使用查询在数据库中删除行?

huangapple go评论90阅读模式
英文:

How can I delete rows in a database using a query?

问题

有一个具有属性Role的用户,默认为TENANT,使用一个查询,我们将他设置为LANDLORD,然后在HOUSE表中,他添加了一个带有各种属性(如描述、价格、城市ID等)的公寓。但突然间,这个用户想要将自己从LANDLORD状态移除,从数据库中删除他的公寓,然后再次变回普通的TENANT状态。在这种情况下,如何删除他拥有公寓的信息呢?如果他有公寓,就需要将它们删除;如果没有,只需要将用户的状态更改为TENANT即可。

起初的想法是将其属性赋值为零,但如果我们只是将其清零,这似乎有些奇怪,因为表会开始变得混乱。还有一个状态选项:ACTIVEBANNED,但我不喜欢这个选项,因为他的公寓实际上是不需要的。

代码如下所示:

@PutMapping ("/{id}")
@PreAuthorize("hasAuthority('landlord: write')")
public void TenantPostAdd(@PathVariable(value = "id") Long id) {
    User user = userRepository.findById(id).orElseThrow();
    Role role = Role.TENANT;
    user.setRole(role);
    House house = houseRepository.findById(id).orElseThrow();
    house ... // 这里是什么
}

完整代码

英文:

There is a user with the attribute Role, by default TENANT, using a query we set him LANDLORD and in theHOUSE table he adds an apartment with various attributes: description, price, city_id and others. But suddenly this user wanted to remove himself from the status of LANDLORD, delete his apartments from our database and again become justTENANT, how in this case can I delete the information that he has apartments? How to do it, if he has apartments, then they need to be deleted, if not, then just change the user's status to TENANT?

At first there was an idea to assign a zero value, but it seemed strange to me if we just zeroed it out, because then the table will start to get cluttered. There is also a status option: ACTIVE or BANNED, but I don't like this option, because his apartment is still not needed.

The code looks like this:

@PutMapping ("/ {id}")
@PreAuthorize ("hasAuthority ('landlord: write')")
public void TenantPostAdd (@PathVariable (value = "id") Long id) {
    User user = userRepository.findById (id) .orElseThrow ();
    Role role = Role.TENANT;
    user.setRole (role);
    House house = houseRepository.findById (id) .orElseThrow ();
    house ... // what's here
}

Full Code

答案1

得分: 0

以下是翻译好的内容:

构建这种级别的基础设施,我会有很多问题需要询问才能做出建议。我希望也能看到当前的数据库架构。您还要求具有删除的能力,这可能会变成一个问题。如果您认为客户可能会再次更改角色,您可能希望考虑保留数据。那种信息是基于协议的条款的。

您考虑过构建类似这样的东西吗?

绝对值(数值) 模式

0 无权限 --- 等等...

https://www.guru99.com/file-permissions.html

这可能是预处理语句的问题,语句中可能没有适当的连接。我认为您应该再次查看数据库架构。

英文:

To build this level of infrastructure, there are a lot of questions I would have to ask to recommend something. I'd want to see the current database schema as well. Your also requesting the ability to delete which can become problematic. You may want to consider leaving data if you believe that the customer may change roles again. That kind of information is based off of the terms of agreement.

Have you considered building something like this?

Absolute(Numeric) Mode

0 No Permission --- etc...

https://www.guru99.com/file-permissions.html

This could be a prepared statement issue with not the appropriate joins occurring in the statement. I believe you should take another look over your database schema.

huangapple
  • 本文由 发表于 2020年9月28日 20:13:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/64101982.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定