Vapor 3 Fluent MySQL:保存模型为auto_increment增加了10

问题描述 投票:1回答:1

当我将对象保存到数据库时,它以+10存储对象的id。表的自动增量默认增加1.当我手动向数据库添加内容时,它将以1递增。

我错过了(或我的错误,设置)FluentMySQL的设置?

  func storeOrUpdateItemRecord(_ item: FetcherItem, store: Store, on conn: DatabaseConnectable) throws -> EventLoopFuture<Item> {
    guard let storeId = store.id else {
      throw Abort(.internalServerError)
    }

    let calendar = Calendar.current
    let fromDate = calendar.startOfDay(for: Date())
    guard let endDate = calendar.date(bySettingHour: 23, minute: 59, second: 59, of: Date()) else { throw Abort(.internalServerError) }

    return Item.query(on: conn)
      // Bunch of filters
      .filter(\.scheduledDate < endDate)
      .all()
      .flatMap(to: Flight.self) { flights in
        if var firstItem = flights.first {
          debugPrint("Found item, updating...")
          // Update a bunch of values
          return firstItem.save(on: conn)
        }

        debugPrint("Did not find item, saving new...")
        return item.toItem(forStore: store).save(on: conn)
    }
  }

toItem func什么也没做,然后发起一个新的Item

extension FetcherItem {

  func toItem(forStore store: Store) -> Item {
    return Item.init(
      id: nil,
      // Set a bunch of values
      actualDate: actualDateTime)
  }
}

正如你所看到的,id设置为nil ...我猜想在进行插入查询时我应该使用null存储。

数据库中的结果:

MySQL db result

我错过了什么?

更新:在遵循this page的建议之后,我已经为查询添加了日志记录......它们似乎可以正常工作。 fluent运行查询的示例:

[mysql] [2019-03-06 08:49:44 +0000] INSERT INTO `Items` (`company`, `name`, `storeId`, [...] `scheduledDate`) VALUES (?, ?, ?, ?, ?, ?, ?, ?) [string("A company"), string("abc123"), integer8(1), [...] time(2019-3-6 16:25:0.0)]

如你所见,它没有以任何方式设置id。也许是MySQL问题?

mysql swift vapor vapor-fluent
1个回答
1
投票

发现了问题。 MySQL已将@@auto_increment_increment设置为10,因此跳跃。

更多信息:

如果遇到此问题,请运行以下命令(source):

  • SELECT @@auto_increment_increment

并设置它:

  • SET @@auto_increment_increment=1

result

© www.soinside.com 2019 - 2024. All rights reserved.