I want to check diferent values in the DB and create a new value, so i need to query and i don't know if i have to create a session in my SQLAlchemy class or how do i have to do it? using session like a global?, i didn't find in documentation.
Somethin like this:
class MyClass(Base):
__tablename__ = 'my_class'
__table_args__ = (UniqueConstraint('key', 'key2'),
{}
)
id = Column(Integer, Sequence('my_class_id'), primary_key=True)
key = Column(String(30), nullable= False) #unique together key2
key2 = Column(String(30), nullable = False)
value = Column(Integer, nullable=False)
def __init__(self, key, key2):
#check if exist key and key2
values = session.query(MyClass.value).filter(MyClass.key == self.key).\
filter(MyClass.key2 == self)
if values:
raise IntegrityError
#get biggest value
value = session.query(MyClass.value).filter(MyClass.key = self.key).order_by(asc(MyClass.value)) #I'm not shure if i need 'asc'
#no value new key and key2
if not value:
self.key = key
self.key2 = key2
self.value = '000'
return
#i used a single table beacuse is easier to understand
#in this example
self.key = key
self.key2 = key
self.value = increment(value.first())
I'm using SQLALchemy 6.2 and declarative
Thanks