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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:26:04+00:00 2026-05-23T18:26:04+00:00

Just a heads up, I’m new to programming in general so I’m guessing I’m

  • 0

Just a heads up, I’m new to programming in general so I’m guessing I’m missing something simple. I’ve been trying to follow the official tutorial here and I’m at the Building Relationships section. It works up to the last session.commit() when it says there is no ‘addresses’ table. This is what I think the code should be doing;

  1. Address sets up a table.
  2. metadata.create_all(engine) creates the table in the current session.
  3. I set up the new user ‘jack’ including some addresses.
  4. I try to add ‘jack’, then commit.

I’m unsure what I’m doing wrong, I’d appreciate any help.

from sqlalchemy import Table, Column, Integer, String, MetaData, ForeignKey, create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, relationship, backref

Base = declarative_base()
metadata = MetaData()
engine = create_engine('sqlite:///:memory:', echo=False)

Session = sessionmaker(bind=engine)
session = Session()

users_table = Table('users', metadata,
                    Column('id', Integer, primary_key=True),
                    Column('name', String),
                    Column('fullname', String),
                    Column('password', String)
                    )



class User(Base):
    __tablename__ = 'users'

    id = Column(Integer, primary_key=True)
    name = Column(String)
    fullname = Column(String)
    password = Column(String)

    def __init__(self, name, fullname, password):
        self.name = name
        self.fullname = fullname
        self.password = password

    def __repr__(self):
       return "<User('%s', '%s', '%s')>" % (self.name, self.fullname, self.password)



class Address(Base):
    __tablename__ = 'addresses'
    id = Column(Integer, primary_key=True)
    email_address = Column(String, nullable=False)
    user_id = Column(Integer, ForeignKey('users.id'))

    user = relationship(User, backref=backref('addresses', order_by=id))

    def __init__(self, email_address):
        self.email_address = email_address

    def __repr__(self):
        return "<Address('%s')>" % self.email_address



metadata.create_all(engine)

session.add_all([User('jim', 'Jim Gregson', 'secretword'),
                 User('john', 'John Smith', 'password'),
                 User('jane', 'Jane Doe', 'qweasdzxc'),
                 User('bob', 'Bob Johnson', '122765')])

session.commit()

for instance in session.query(User).order_by(User.id): print instance



jack = User('jack', 'Jack Bean', 'gjffdd')
print "jack.addresses: ", jack.addresses
jack.addresses = [Address(email_address='jack@google.com'), Address(email_address='j25@yahoo.com')]
print "jack.addresses: ", jack.addresses
print "jack.addresses[1].user: ", jack.addresses[1].user

session.add(jack)
session.commit()

Error below when 'session.commit()' is called;
sqlalchemy.exc.OperationalError: (OperationalError) no such table: addresses u'INSERT INTO addresses (email_address, user_id) VALUES (?, ?)' ('jack@google.com', 5)
  • 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-23T18:26:04+00:00Added an answer on May 23, 2026 at 6:26 pm

    The Base object contains it’s own metadata instance that it adds more metadata to as your entities are defined. So, when the User class is defined, the Base object realizes this, and adds all of the metadata for the User class to it’s own metadata instance (Base.metadata).

    In your script, you’ve created your own metadata instance (metadata = MetaData()), and then later on used that empty metadata to create your tables (metadata.create_all(engine)).

    Don’t create your own metadata, and use the base one instead:

    Base.metadata.create_all(engine)
    

    Edit:

    Another note: You probably would have received the same error for the users table, but it seems that you have defined the users table metadata in what I called in my answer the “empty” metadata. You don’t need to do this; defining the User table in the way that you’ve done it (defining the “User” class) is good enough to do everything you did when you said “users_table = Table(…)”. You’re basically doing the same thing in two ways, and you really only need to do one.

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

Sidebar

Related Questions

I am just trying to get my head around simple view switching for the
Just as a little heads up, I'm completely new to this whole coding thing
I've been trying to Google and find some guides or heads-up on how to
Just wondering if anyone knew off the top of their heads if there was
I'm just getting my head round C#. I've been creating classes and objects so
Just trying to get my head around Generics by reading this enlightening article by
Just trying to get my head round Spring and figuring out how I wire
Just trying to get my head around what can happen when things go wrong
Just trying to still get my head around IOC principles. Q1: Static Methods -
I am just trying to get my head around various pointer concepts and I

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.