If you use SQLAlchemy occasionally, you will forget how to use it, so an entry to take notes every time you use it
instance = sessoin.query(Model).filter_by(Schema=Value)
instance = sessoin.query(Model).filter(Model.Schema==Value) #Same as above
instance.one() #Returns an object when there is only one. If not(NoResultFound)Or if there are multiple(MultipleResultsFound)An error will occur.
instance.first() #If not, None is returned. If there is more than one, something one object is returned
instance.all() #List returns
[m.Schema for m for instance] #Like, instance is iterable.
instance.count() #The number is returned
model_data = Model(Schema=Value)
sessoin.add(model_data)
model_data.Schema2 = Value2
transaction.commit()
model_data = sessoin.query(Model).filter_by(Schema=Value).first()
if model_data is None:
model_data = Model(Schema=Value)
sessoin.add(model_data)
model_data.Schema2 = Value2
transaction.commit()
Recommended Posts