Note that there are some explanatory texts on larger screens.

plurals
  1. POFollowing the official ORM tutorial and I'm running into an error
    primarykey
    data
    text
    <p>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 <a href="http://www.sqlalchemy.org/docs/orm/tutorial.html" rel="nofollow">tutorial here</a> and I'm at the <a href="http://www.sqlalchemy.org/docs/orm/tutorial.html#building-a-relationship" rel="nofollow">Building Relationships</a> 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;</p> <ol> <li>Address sets up a table.</li> <li>metadata.create_all(engine) creates the table in the current session.</li> <li>I set up the new user 'jack' including some addresses.</li> <li>I try to add 'jack', then commit.</li> </ol> <p>I'm unsure what I'm doing wrong, I'd appreciate any help.</p> <pre><code>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 "&lt;User('%s', '%s', '%s')&gt;" % (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 "&lt;Address('%s')&gt;" % 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) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload