Inserts a record for the given model.

    If model's primary key is auto, the primary key will be set appropriately.

def insert_model(self, model, *, upsert=None):
    """Inserts a record for the given model.

    If model's primary key is auto, the primary key will be set appropriately.
    """
    pkname = model.primary_key_name
    include_keys = set(model.attrs.keys()).difference(model.exclude_keys_sql)

    if model.primary_key_is_auto:
      if pkname in include_keys:
        include_keys.remove(pkname)
    else:
      if isinstance(pkname, str):
        include_keys.add(pkname)
      else:
        include_keys.update(set(pkname))

    data = model.to_dict(include_keys=include_keys)
    returnings = []

    if model.primary_key_is_auto:
      returnings.append(pkname)
    if model.timestamps:
      returnings.extend(ts_name for ts_name in model.timestamps if ts_name)

    returning = ", ".join(returnings)
    cr = self.insert(model.table_name, data, returning=returning, upsert=upsert)

    if self.core.supports_returning_syntax:
      if returning:
        rec = cr.fetchone()
        if rec:
          for idx, attr_name in enumerate(returnings):
            setattr(model, attr_name, rec[idx])
    else:
      if model.primary_key_is_auto:
        setattr(model, model.primary_key_name, cr.lastrowid)

    return model