【问题标题】:How to insert into table with max(ordering) + 1 in ordering column?如何在排序列中插入 max(ordering) + 1 的表?
【发布时间】:2021-08-30 12:32:54
【问题描述】:

我在 Postgres 中有一个带有 ordering 列(整数)的 chapters 表。如何在柴油中插入/创建ordering 设置为MAX(ordering) + 1 的新行?我尝试使用原始 sql 如下:

INSERT INTO chapters
(id, name, user_id, book_id, ordering, date_created, date_modified)
SELECT ?, ?, ?, ?, IFNULL(MAX(ordering), 0) + 1, ?, ?
FROM chapters
WHERE book_id = ?;

并通过以下方式插入:

 let id = Uuid::new_v4().to_string_id();
    let now = Utc::now().naive_utc();
    diesel::sql_query(INSERT_CHAPTER)
        .bind::<Text, _>(&id)
        .bind::<Text, _>(name)
        .bind::<Text, _>(uid)
        .bind::<Text, _>(bid)
        .bind::<Timestamp, _>(&now)
        .bind::<Timestamp, _>(&now)
        .bind::<Text, _>(bid)
        .execute(conn)?;

它适用于 Sqlite3 后端,但在 Pg 后端失败并显示以下错误消息:

"syntax error at or near \",\""

【问题讨论】:

    标签: rust rust-diesel


    【解决方案1】:

    使用 $N 作为 Postgres 的占位符,使用 ? 作为 sqlite3 的占位符。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-21
      • 1970-01-01
      • 1970-01-01
      • 2014-06-27
      • 1970-01-01
      • 2013-05-21
      • 2018-04-02
      相关资源
      最近更新 更多