I was looking for a simple way to create an object only if a specific combination of objects doesn't exist.
Example: I have this class. And my goal is to only Insert a new one if the object in "facts" isn't the same.
class Facts(Base):
__tablename__ = "facts"
id = sqlColumn(Integer, primary_key=True)
fact = sqlColumn(String(500))
created_at = sqlColumn(DateTime)
updated_at = sqlColumn(DateTime)
I already found this code at Does SQLAlchemy have an equivalent of Django's get_or_create?
def get_or_create(session, model, **kwargs):
instance = session.query(model).filter_by(**kwargs).one_or_none()
if instance:
return instance
else:
instance = model(**kwargs)
session.add(instance)
return instance
But this doesn't really fit my needs, since the object gets created anyways because of the different timestamp at "created at".
Furthermore I would like to add as much filter elements as I want.