【问题标题】:How do I upsert in sqlite using diesel?如何使用柴油在 sqlite 中进行 upsert?
【发布时间】:2021-10-07 09:22:21
【问题描述】:

我正在将 rust 应用程序从 Postgres 迁移到 Sqlite。但是我遇到了 upsert 的问题

以下代码

diesel::insert_into(dialogs_table)
            .values(dialog)
            .on_conflict(user_id_column)

给我这个:

error[E0599]: no method named `on_conflict` found for struct InsertStatement in the current scope

我觉得这很奇怪,因为它适用于 Postgres。在这里使用 upsert 的可能解决方案是什么?

【问题讨论】:

  • 请问upsert 是什么?
  • @MarkSetchell 请参阅sqlite.org/lang_upsert.html(这是我更喜欢编写自己的 SQL 而不是使用 ORM 或其他查询生成器的原因之一;您实际上可以使用您想要的所有数据库功能,而不是受到限制到 ORM 层理解的内容)
  • @Shawn 谢谢 - 我没听说过。
  • @Shawn 好像把婴儿和洗澡水一起扔了。几乎所有的 ORM 或查询构建器都有允许编写原始 SQL 的逃生舱口。 diesel certainly does。当然,这里的情况是数据库迁移/跨数据库支持,这绝对是在任何地方使用原始 sql 手动完成的。

标签: sqlite rust rust-diesel


【解决方案1】:

在这里使用 upsert 的可能解决方案是什么?

对 sqlite upserts 的支持已合并 in early 2020(经过一个非常漫长且相当艰苦的 PR 过程),但它显然包括重大更改,所以我的理解是它只会在 Diesel 2.0 中可用。

因此,您可能的解决方案是:

  1. 不要使用 upserts

  2. perform your query by hand

  3. use git diesel

【讨论】:

  • 是的,我已经迁移到了 git 柴油机,一切都很好,感谢您的回复 :)
猜你喜欢
  • 2021-11-07
  • 1970-01-01
  • 2022-12-10
  • 2020-04-14
  • 2023-03-27
  • 2021-08-21
  • 2020-05-09
  • 2022-12-10
  • 2020-02-06
相关资源
最近更新 更多