Skip to content

Instantly share code, notes, and snippets.

@estebanafonso
Created September 1, 2015 23:49
Show Gist options
  • Save estebanafonso/bc48d460d49b0c376261 to your computer and use it in GitHub Desktop.
Save estebanafonso/bc48d460d49b0c376261 to your computer and use it in GitHub Desktop.
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
engine = create_engine('postgresql://ubuntu:thinkful@localhost:5432/tbay')
Session = sessionmaker(bind=engine)
session = Session()
Base = declarative_base()
from datetime import datetime
from sqlalchemy import Column, Integer, String, DateTime, Float, ForeignKey
from sqlalchemy.orm import relationship, backref
class Item(Base):
__tablename__ = "item"
id = Column(Integer, primary_key=True)
name = Column(String, nullable=False)
description = Column(String)
start_time = Column(DateTime, default=datetime.utcnow)
seller_id = Column(Integer, ForeignKey('user.id'))
class User(Base):
__tablename__ = "user"
id = Column(Integer, primary_key=True)
username = Column(String, nullable=False)
password = Column(String, nullable=False)
items = relationship("Item", backref="seller")
class Bid(Base):
__tablename__ = "bid"
id = Column(Integer, primary_key=True)
price = Column(Float, nullable=False)
Base.metadata.create_all(engine)
ball = Item(name="ball")
bill = User(username="Bill",password="asf",items=[ball])
#ball = Item()
#ball.seller = bill
bat = Item(name="bat")
bill.items.append(bat)
session.add_all([bill,ball])
session.commit()
#for item in bill.items_selling:
# print item.name
#print bat.seller_id.username
for item in bill.items:
print item.name
print bat.seller.username
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment