code: def upsert(cls, name, **fields):
          
          instance = cls.get(name)
          if instance:
              instance._set_fields(fields)
          else:
              instance = cls(name=name, **fields)
              instance = failsafe_add(cls.query.session, instance, name=name)
          return instance
docs: Insert or update an instance