当我将对象保存到数据库中时,它会将对象的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
...我猜应该在进行插入查询时使用null
存储。
数据库中的结果
我想念什么?
更新: 在遵循this page的建议之后,我为查询添加了日志记录...,它们似乎可以正常工作。 流利的查询示例:
[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问题?
答案 0 :(得分:1)
Found the problem. MySQL has set the @@auto_increment_increment
to 10
, hence the jumping.
More information here:
If you have this problem, run the following command (source):
SELECT @@auto_increment_increment
and to set it:
SET @@auto_increment_increment=1