简体   繁体   English

删除蒸气流枢轴

[英]Deleting a Vapor Fluent Pivot

I'm using Vapor to write a server side Swift API, with Fluent to access my Postgres database. 我正在使用Vapor编写服务器端Swift API,并使用Fluent访问我的Postgres数据库。 It's working well, but I have a couple of questions about Pivots. 它运行良好,但是我对Pivots有几个问题。

I have a Feed model and an Article model. 我有一个Feed模型和一个Article模型。 The Feed contains many Articles, and an Article can appear in many Feeds. Feed中包含许多文章,并且文章可以出现在许多Feed中。 This is a Sibling relationship as defined by Fluent and I have a Pivot that works nicely for adding Articles to Feeds. 这是Fluent定义的同级关系,我有一个Pivot,可以很好地将文章添加到Feed中。 However, it is currently possible to add the same Article to the same Feed more than once, since the primary key on the Feed_Article Pivot table is it's own unique id field. 但是,由于Feed_Article Pivot表的主键是它自己的唯一ID字段,因此当前可以多次将同一Article添加到同一Feed中。

I have two questions: 我有两个问题:

  1. How can I identify if a Pivot already exists? 如何确定数据透视是否已经存在?

    • Do I need to get the Feed's sibling Articles and go through them to see if the Article's id is already in there? 我是否需要获取Feed的同级文章,并仔细检查它们的ID是否已在其中? Seems a little painful. 似乎有点痛苦。
  2. How can I delete a Pivot relationship? 如何删除数据透视关系?

    • Ie. IE浏览器。 How can I remove an Article from a Feed? 如何从Feed中删除文章?
    • At the moment the only way I can do it is by writing the raw SQL to delete from Feed_Article, but that seems wrong. 目前,我唯一的方法是编写要从Feed_Article中删除的原始SQL,但这似乎是错误的。

I'm using the VaporPostgreSQL driver, so I'm not sure that all of the Fluent implementations are available to me (eg. I can't use UUID ids as they are not part of this driver as yet.) 我正在使用VaporPostgreSQL驱动程序,所以我不确定所有Fluent实现都对我可用(例如,我不能使用UUID id,因为它们目前还不是该驱动程序的一部分。)

Thanks all 谢谢大家

--TJ --TJ

Additional: 额外:

I've ended up creating a Pivot extension. 我最终创建了Pivot扩展。 It works, but it feels like there should be a nicer way to do this. 它可以工作,但是感觉应该有更好的方法来做到这一点。 Anyway, sharing in case it helps others. 无论如何,分享以防他人。

extension Pivot {

static func remove(leftId: Node?, rightId: Node?) throws {
    /// Get the database driver
    guard let db = drop.database?.driver as? PostgreSQLDriver else {
        Logger.error("Failed to get database")
        return
    }

    /// Check that we have valid id's
    guard let leftId = leftId?.int, let rightId = rightId?.int else {
        Logger.error("Invalid id's")
        return
    }

    /// Delete the rows
    let sql = "DELETE FROM \(name) WHERE \(left.name)_\(left.idKey) = \(leftId) AND \(right.name)_\(right.idKey) = \(rightId)"
    Logger.debug("SQL: \(sql)")
    try db.raw(sql)
}

}

You should be able to query, delete, etc pivots like any other Fluent object. 您应该能够像其他Fluent对象一样查询,删除等枢轴。

For example: 例如:

try Pivot<User, Pet>.query().fiter("user_id", x).delete()

In Fluent 2 (2.0.0-beta.1 at the time of writing) there are convenience methods on the Siblings relation for doing this. 在Fluent 2(撰写本文时为2.0.0-beta.1)中,在Siblings关系上有方便的方法可以做到这一点。

For example: 例如:

try user.pets.add(newPet)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM