Advertisement
Guest User

Untitled

a guest
Nov 21st, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. from sqlalchemy import create_engine
  2. from sqlalchemy import Column, Integer, String, ForeignKey, DateTime
  3. from sqlalchemy.orm import sessionmaker
  4. from sqlalchemy.orm import relationship
  5. from sqlalchemy.ext.declarative import declarative_base
  6.  
  7. engine = create_engine('postgresql://ubuntu:thinkful@localhost:5432/tbay')
  8. Session = sessionmaker(bind=engine)
  9. session = Session()
  10. Base = declarative_base()
  11.  
  12. from datetime import datetime
  13.  
  14. class Item(Base):
  15. __tablename__ = "items"
  16.  
  17. id = Column(Integer, primary_key=True)
  18. name = Column(String, nullable=False)
  19. description = Column(String)
  20. start_time = Column(DateTime, default=datetime.utcnow)
  21. #relationship establishing one user can "own" many items
  22. user_id = Column(Integer, ForeignKey('users.id'), nullable=False)
  23. #relationship extablishing that one item can have many bi
  24. bids = relationship("Bid", backref="item")
  25.  
  26. class User(Base):
  27. __tablename__ = "users"
  28.  
  29. id = Column(Integer, primary_key=True)
  30. username = Column(String, nullable=False)
  31. password = Column(String, nullable=False)
  32. #one to many users to items
  33. items = relationship("Item", backref="user")
  34. #one to many users to bids
  35. bids = relationship("Bid", backref="user")
  36.  
  37. class Bid(Base):
  38. __tablename__ = "bids"
  39. id = Column(Integer, primary_key=True)
  40. price = Column(Integer, nullable=False)
  41. #one to many users to bids
  42. user_id = Column(Integer, ForeignKey('users.id'), nullable=False)
  43. item_id = Column(Integer, ForeignKey('items.id'), nullable=False)
  44.  
  45. Base.metadata.create_all(engine)
  46.  
  47. jeff = User()
  48. jeff.username = "jmsword"
  49. jeff.password = "Alyeska9"
  50.  
  51. ryan = User()
  52. ryan.username = "ryry"
  53. ryan.password = "mustang"
  54.  
  55. tara = User()
  56. tara.username = "tara"
  57. tara.password = "kia"
  58.  
  59. baseball = Item(name = "baseball", description = "McGuires homerun ball", user=jeff)
  60.  
  61. bid1 = Bid(price = 10, user=ryan, item=baseball)
  62. bid2 = Bid(price = 12, user=tara, item=baseball)
  63. bid3 = Bid(price = 14, user=ryan, item=baseball)
  64. bid4 = Bid(price = 16, user=tara, item=baseball)
  65.  
  66. session.add_all([jeff, ryan, tara, baseball, bid1, bid2, bid3, bid4])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement