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 8845795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:49:08+00:00 2026-06-14T11:49:08+00:00

Here’s a pretty interesting question… I’m writing a program to manipulate several remote databases

  • 0

Here’s a pretty interesting question…
I’m writing a program to manipulate several remote databases with same schema via SQLAlchemy. The connection string is not static — it is stored in a local SQLite database.
I need to create a session at runtime, and use that session to reflect the database schema. All databases have same schema.
I have already know the table names.
Anyway to implement this?

def connect(connstr):
    engine = create_engine(connstr)
    metadata = MetaData(bind=engine)
    session = create_session(bind=engine)
    return session

class User(Base):
    # How it's possible to create a dummy model class without predefined metadata?
    __table__ = Table('users', metadata, autoload=True)

EDIT: finally I solved this problem by myself:

Base = declarative_base()

def connect(connstr):
    engine = create_engine(connstr)
    metadata = MetaData(bind=engine)
    session = create_session(bind=engine)
    return metadata, session

def get_model(metadata, modelname, tablename):
    cls = type(modelname, (Base,), dict(
        __table__ = Table(tablename, metadata, autoload = True)
    ))
    return cls

However, @Gary van der Merwe’s answer is pretty cool!

  • 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-14T11:49:10+00:00Added an answer on June 14, 2026 at 11:49 am

    You have to manually map the orm classes to the reflected tables.

    Assuming a database with the following structure:

    CREATE TABLE "group" (
            id INTEGER NOT NULL, 
            name VARCHAR(50), 
            PRIMARY KEY (id)
    )
    
    CREATE TABLE user (
            id INTEGER NOT NULL, 
            name VARCHAR(50), 
            password VARCHAR(12), 
            group_id INTEGER, 
            PRIMARY KEY (id), 
            FOREIGN KEY(group_id) REFERENCES "group" (id)
    )
    

    you can do:

    #!/usr/bin/env python
    
    from sqlalchemy import create_engine, MetaData
    from sqlalchemy.orm import mapper, Session, relationship
    
    
    class Group(object):
        def __init__(self, name):
            self.name = name
    
    class User(object):
        def __init__(self, name, password, group):
            self.name = name
            self.password = password
            self.group = group
    
        group = relationship(Group)
    
    def connect(url):
        engine = create_engine(url, echo=True)
        metadata = MetaData(engine, reflect=True)
    
        # Do this for each table
        mapper(User, metadata.tables['group'])
        mapper(Group, metadata.tables['user'])
    
    
        session = Session(bind=engine)
        return session
    
    
    session = connect('sqlite:///test.db')
    finance_group = Group('finance')
    session.add(finance_group)
    session.add(User('joe', 'password', finance_group))
    session.commit()
    print(session.query(User).all())
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the scenario. I'm writing my geo-ruby oracle adapter for Ruby On Rails
Here's what I'm trying to accomplish with this program: a recursive method that checks
Here's what I'm doing. I want to upload multipart file via Ajax to my
Here is my program to find all the subsets of given set. To solve
Here are some facts about my app followed by a question My app has
Here is the scenario: I'm writing an app that will watch for any changes
Here's an interesting problem. On a recently installed Server 2008 64bit I opened IE
Here a simple question : What do you think of code which use try
Here's a quick question. When you use format specifiers in the string you want
Here's the view: @if (stream.StreamSourceId == 1) { <img class=source src=@Url.Content(~/Public/assets/images/own3dlogo.png) alt= /> }

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.