当我将对象保存到数据库时,它将对象的id存储为+10。默认情况下,表的自动增量为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
函数不再执行任何操作,然后启动一个新的Item
:extension FetcherItem {
func toItem(forStore store: Store) -> Item {
return Item.init(
id: nil,
// Set a bunch of values
actualDate: actualDateTime)
}
}
如您所见,
id
设置为nil
。。。我猜在进行insert查询时应该使用null
存储它。数据库中的结果:
我错过了什么?
更新:
根据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已将@@auto_increment_increment
设置为10
,因此出现跳跃。
更多信息请点击此处:
https://stackoverflow.com/a/31969004/406677
https://dev.mysql.com/doc/refman/5.6/en/innodb-auto-increment-handling.html
如果有此问题,请运行以下命令(source):SELECT @@auto_increment_increment
设置它:SET @@auto_increment_increment=1