1

When I am saving my object to the database, it stores the id of the object with +10. The autoincrement for the table increments by default with 1. When I manually add something to the database, it'll increment with 1.

Am I missing (or my misstake, setting) a setting for 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)
    }
  }

The toItem func does nothing more then initiate a new Item:

extension FetcherItem {

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

As you can see, the id is set to nil... which I would guess should store using null while making the insert query.

The result in the database:

MySQL db result

What am I missing?

Update: After following suggestions from this page I've added logging for the queries... and they seem to work as they should. Example of what fluent runs for query:

[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)]

As you can see, it doesn't set the id in any way. Maybe a MySQL problem?

4

1 回答 1

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

result

于 2019-03-06T11:36:21.180 回答