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

问题描述 投票:0回答:2

我使用的是光滑的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放在列中,而不是使用数据库提供的递增值。

上面的代码执行正常。但是当我进入DB时,我发现ID是1.因此它不会强制id为0.它仍然使用数据库提供的值。

scala slick slick-3.0
2个回答
0
投票

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


0
投票

这段代码正在运行

对于自动增量,在scala中为相应的参数值添加零,并使用自动增量字段[id]创建表

**/*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)
© www.soinside.com 2019 - 2024. All rights reserved.