Note that there are some explanatory texts on larger screens.

plurals
  1. POSqlalchemy doesn't create schema from metadata
    primarykey
    data
    text
    <p>I want to create a database schema out and insert data with SQLalchemy. But for some reason SQLAlchemy doesn't apply the schema in <code>create_fixture.py</code>. SQLAlchemy raises an error when I commit my session because it can't find any tables.</p> <p>Here a simplified version of my code. My <code>__init__.py</code>:</p> <pre><code> from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker, scoped_session from sqlalchemy.ext.declarative import declarative_base engine = create_engine('postgresql://postgres@localhost/syncer', echo=True) Base = declarative_base(bind=engine) session = scoped_session(sessionmaker(engine)) </code></pre> <p><code>entity.py</code>:</p> <pre><code> from sqlalchemy import Column, String, Integer from __init__ import Base, session class Zone(Base): __tablename__ = 'zone' id = Column(Integer, primary_key=True) name = Column(String(50)) </code></pre> <p><code>create_fixture.py</code></p> <pre><code> from __init__ import Base, session from entity import Zone from sqlalchemy import create_engine Base.metadata.create_all() zone = Zone('zone_1') session.add(zone) session.commit() </code></pre> <p>The output</p> <pre><code> 2013-09-13 20:06:12,340 INFO sqlalchemy.engine.base.Engine select version() 2013-09-13 20:06:12,341 INFO sqlalchemy.engine.base.Engine {} 2013-09-13 20:06:12,345 INFO sqlalchemy.engine.base.Engine select current_schema() 2013-09-13 20:06:12,345 INFO sqlalchemy.engine.base.Engine {} 2013-09-13 20:06:12,352 INFO sqlalchemy.engine.base.Engine BEGIN (implicit) 2013-09-13 20:06:12,354 INFO sqlalchemy.engine.base.Engine INSERT INTO zone (name) VALUES (%(name)s) RETURNING zone.id 2013-09-13 20:06:12,354 INFO sqlalchemy.engine.base.Engine {'name': 'zone_1'} 2013-09-13 20:06:12,356 INFO sqlalchemy.engine.base.Engine ROLLBACK sqlalchemy.exc.ProgrammingError: (ProgrammingError) relation "zone" does not exist LINE 1: INSERT INTO zone (name) VALUES ('zone_1') RETURNING zone.id </code></pre> <p>Why doesn't apply SQLAlchemy my schema? How can I fix it so it does apply my schema?</p>
    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.
    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