在 mysql 中为自动递增列插入 0

Inserting 0 for an auto-incrementing column in mysql

我正在使用 slick 3.2.1

我需要在自动递增的列中插入“0”(MySQL)。

这段代码是我写的

val foo = Foo(0, "FOO")
val fooQuery = Tables.FooTable forceInsert  foo
Await.result(db.run(fooQuery), Duration.Inf)

我希望 forceinsert 将准确的值 0 放入列中,而不是使用数据库提供的增量值。

上面的代码执行得很好。但是当我进入数据库时​​,我看到 ID 是 1。所以它不会强制 ID 为 0。它仍在使用数据库提供的值。

如果您的值低于现有值,您将无法插入任何自动递增的列。如果我相信 table 是空的,你可以这样做。

此代码有效。

对于自动递增,将零添加到 scala 中的相应参数值,并使用自动递增字段[id]

创建table
**/*Table creation*/**

CREATE TABLE `recommendation` 
(
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `product_id` int(11) DEFAULT NULL,
    `rec_product_id` int(11) DEFAULT NULL,
    `color_id` int(11) DEFAULT '1',
    `uiposition` int(11) DEFAULT '0',

    PRIMARY KEY (`id`)
)

/* scala Code */

val insertSql = """
                      |INSERT INTO `wellness`.`recommendation` (`id`, `product_id`, `rec_product_id`, `color_id`, `uiposition`)
                      |VALUES (?,?,?,?,?)
                    """.stripMargin


    val preparedStmt: sql.PreparedStatement = conn.prepareStatement(insertSql)

    preparedStmt.setInt (1, 0)//Auto increment in scala**
    preparedStmt.setInt (2, vproid)
    preparedStmt.setInt (3, recoid.toInt)
    preparedStmt.setInt    (4, 1)
    preparedStmt.setInt    (5, uipos)