code: def get_or_create(session: Session,
                    model: DeclarativeMeta,
                    defaults: Dict[str, Any] = None,
                    **kwargs: Any) -> Tuple[Any, bool]:
      
      instance = session.query(model).filter_by(**kwargs).first()
      if instance:
          return instance, False
      else:
          params = dict((k, v) for k, v in kwargs.items()
                        if not isinstance(v, ClauseElement))
          params.update(defaults or {})
          instance = model(**params)
          session.add(instance)
          return instance, True
docs: Fetches an ORM object from the database, or creates one if none existed.
  
      Args:
          session: an SQLAlchemy :class:`Session`
          model: an SQLAlchemy ORM class
          defaults: default initialization arguments (in addition to relevant
              filter criteria) if we have to create a new instance
          kwargs: optional filter criteria
  
      Returns:
          a tuple ``(instance, newly_created)``
  
      See http://stackoverflow.com/questions/2546207 (this function is a
      composite of several suggestions).