title: How to insert into table with max(ordering) + 1 in ordering column?
body: <p>I have a <code>chapters</code> table with a <code>ordering</code> column(integer) in Postgres. How to insert/create a new row with <code>ordering</code> set to <code>MAX(ordering) + 1</code> in diesel? I've tried using raw sql as following:</p>
  <pre><code>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 = ?;
  </code></pre>
  <p>and insert into by:</p>
  <pre class="lang-rust prettyprint-override"><code> let id = Uuid::new_v4().to_string_id();
      let now = Utc::now().naive_utc();
      diesel::sql_query(INSERT_CHAPTER)
          .bind::&lt;Text, _&gt;(&amp;id)
          .bind::&lt;Text, _&gt;(name)
          .bind::&lt;Text, _&gt;(uid)
          .bind::&lt;Text, _&gt;(bid)
          .bind::&lt;Timestamp, _&gt;(&amp;now)
          .bind::&lt;Timestamp, _&gt;(&amp;now)
          .bind::&lt;Text, _&gt;(bid)
          .execute(conn)?;
  </code></pre>
  <p>It works on Sqlite3 backend, but fails on Pg backend with following error message:</p>
  <pre class="lang-sh prettyprint-override"><code>&quot;syntax error at or near \&quot;,\&quot;&quot;
  </code></pre>