/
tbay.py
70 lines (51 loc) · 2.2 KB
/
tbay.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from datetime import datetime
from sqlalchemy import Column, ForeignKey, Integer, Float, String, DateTime
from sqlalchemy.orm import relationship
from database import Base, engine
class User(Base):
__tablename__ = "users"
id = Column(Integer, primary_key=True)
username = Column(String, nullable=False)
password = Column(String, nullable=False)
class Item(Base):
__tablename__ = "items"
id = Column(Integer, primary_key=True)
name = Column(String, nullable=False)
description = Column(String)
start_time = Column(DateTime, default=datetime.utcnow)
# One-to-many relationship between User and Item
owner_id = Column(Integer, ForeignKey('users.id'), nullable=False)
owner = relationship('User', backref="items")
class Bid(Base):
__tablename__ = "bids"
id = Column(Integer, primary_key=True)
price = Column(Float, nullable=False)
# One-to-many relationship between Item and Bid
item_id = Column(Integer, ForeignKey('items.id'), nullable=False)
item = relationship('Item', backref="bid")
# One-to-many relationship between User and Bid
bidder_id = Column(Integer, ForeignKey('users.id'), nullable=False)
bidder = relationship('User', backref="bids")
Base.metadata.create_all(engine)
if __name__ == '__main__':
from database import session
from sqlalchemy import desc
beyonce = User(username='bknowles', password='123456')
christina = User(username='caguillera', password='qweasd')
britney = User(username='bspears', password='secret')
microphone = Item(name="microphone",
description="Mika's gold microphone.",
owner=britney)
bids = [
Bid(price=100.0, bidder=christina, item=microphone),
Bid(price=150.0, bidder=beyonce, item=microphone),
Bid(price=200.0, bidder=christina, item=microphone),
Bid(price=201.0, bidder=beyonce, item=microphone),
]
session.add_all([beyonce, christina, britney, microphone] + bids)
session.commit()
highest_bid = (session.query(Bid)
.filter(Bid.item==microphone)
.order_by(desc(Bid.price))
.first())
print(highest_bid.bidder.username)