Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 911973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:18:04+00:00 2026-05-15T17:18:04+00:00

I want to check diferent values in the DB and create a new value,

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T17:18:05+00:00Added an answer on May 15, 2026 at 5:18 pm

    You will have to do your own session management — e.g. define a module-global session object.

    For example, Pylons applications define their session like this:

    from sqlalchemy.orm import scoped_session, sessionmaker
    Session = scoped_session(sessionmaker())
    

    and then later bind it to an engine with

    Session.configure(bind=engine)
    

    Using scoped_session will mean that your code is thread-safe (each thread will use its own session).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I don't want check if an item with a value exists in the arraylist,
I want to check if a Dom has class value attached to it.. Since
I want to create a class which validate different inputs. In Controller, I have
I want to check a Column value when update.If its match insert into another
I have an unbalanced quarterly panel data set with missing values. I want to
Here I have one file which contains some information, and I want to check
i want to create one ssis package which takes values from flat file and
I want to check which port is used, when the port is not explicitly
I want to check checkbox like $('#riderCheck').attr('checked','true') on some condition. but the problem is
I want to check if my many column in datagridview is empty. here my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.