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

  • SEARCH
  • Home
  • 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 7795999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:14:08+00:00 2026-06-01T23:14:08+00:00

I have a little problem regarding arranging of code, which depends on each other

  • 0

I have a little problem regarding “arranging of code, which depends on each other” when setting an sqlalchemy mapping to a sqlite database in python.

The goal is to write a script, whcih satisfies the following conditions:

  1. Gets a filename parameter as command line argument.
  2. Based on the filename it should create an absolute path to the SQLite database.
  3. It should connect to the database and create an engine
  4. It shall reflect the tables in this databases.
  5. It should monkey patch the column id in the table mytable as a primary key column, since the table doesn’t habe a primary key and sqlalchemy requires one.

So I came up with this:

from sqlalchemy import create_engine, Column, Integer
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base  

def create_path(file_name):
    # generate absolute path to file_name

path_to_file = create_path("my_file_name.sqlite")
engine = create_engine('sqlite:///{path}'.format(path=path_to_file), echo=False)
Base = declarative_base()
Base.metadata.create_all(engine)

class MyTable(Base):
    __tablename__ = 'my_table'
    __table_args__ = {'autoload': True}

    id = Column(Integer, primary_key=True) # Monkey patching the id column as primary key.

def main(argv):
    # parse file_name here from argv
    Session = sessionmaker(bind=engine)
    session = Session()
    for row in session.query(MyTable).all():
        print row
    return "Stop!"

if __name__ == '__main__':
    sys.exit(main())

But this is a doomed construction and I don’t see how I could rearrange my code without breaking the dependencies.

  1. To be able to create MyClass I need Base to be defined before MyClass.
  2. To be able to create Base I need engine to be defined before Base.
  3. To be able to create engine I need path_to_file to be defined before engine.
  4. To be able to create path_to_file outside of main() I need create_file() to be defined before path_to_file.
  5. And so on…

Hopefully you see where I am stuck…

Any suggestions?

Edit: By the way, the code works, but only with a hardcoded filename in the top of the script.

  • 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-06-01T23:14:10+00:00Added an answer on June 1, 2026 at 11:14 pm

    I do not see why you could not use the declarative mapping still complety. I think the key to the proper dependencies is to know what to put in the module and what in the script/function. With python, you can easily define the class inside the run_script function.

    from sqlalchemy import create_engine, Column, Integer
    from sqlalchemy.orm import sessionmaker
    from sqlalchemy.ext.declarative import declarative_base
    
    def create_path(filename):
        import os
        fn = os.path.abspath(os.path.join(os.path.dirname(__file__), 'sqlite_files', filename))
        return fn
    
    def run_script(filename):
        path_to_file = create_path(filename)
    
        engine = create_engine('sqlite:///{path}'.format(path=path_to_file), echo=True)
        Session = sessionmaker(bind=engine)
        Base = declarative_base(bind=engine)
    
        # object model
        class MyTable(Base):
            __tablename__ = 'my_table'
            __table_args__ = {'autoload': True}
            id = Column(Integer, primary_key=True) # Monkey patching the id column as primary key
    
        # script itself
        session = Session()
        for row in session.query(MyTable).all():
            print row
        return "Stop!"
    
    def main(argv):
        assert argv, "must specify a database file name"
        run_script(argv[0])
    
    if __name__ == '__main__':
        import sys
        sys.exit(main(sys.argv))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a little question, regarding to slideup and slidedown, the problem is in
I have this little problem, that I cannot figure out which arguments to pass
I have a little piece of code which reads a file and i want
I am writing a little test program, and I have a problem regarding the
I have little problem with a replacement of a little part in an url.
I have little problem in regular expressin creation. Expected input: blahblahblah, blahblahblah, 'blahblahblah', blahblahblah,
I have a little problem with a simple vbScript. The script has to run
I have a little problem with some jquery and http://www.mikage.to/jquery/jquery_history_noc.html The function works great,
I have a little problem where I would like to insert a svn diff
I have a little problem with a query. I'm selecting data using the between

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.