Beispiel #1
0
def get_db():
    db = None
    try:
        db = SessionLocal()
        yield db
    finally:
        db.close()
Beispiel #2
0
    is_active = Column(Boolean, default=True)

    items = relationship('Item', back_populates='owner')

class Item(Base):
    __tablename__ = 'item'

    id = Column(Integer, primary_key=True)
    title = Column(String, index=True)
    description = Column(String, index=True)
    owner_id = Column(Integer, ForeignKey('user.id'))

    owner = relationship('User', back_populates='items')
# %% Create All
Base.metadata.create_all(engine)
db = SessionLocal()

# %% Create User
db_user = User(email='lee3sang', password='******')
db.add(db_user)
db.commit()
db.refresh(db_user)

# %% Create Item
db_item = Item(title='speaker', description='food market', owner_id=1)
db.add(db_item)
db.commit()
db.refresh(db_item)

# %% Get User
# db.rollback()