-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex16_insert_many_rows.py
40 lines (32 loc) · 974 Bytes
/
ex16_insert_many_rows.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
import datetime
import random
import string
import sqlalchemy as sa
from sqlalchemy.orm import sessionmaker
from helpers import Timer
from models import Account, Invoice, Product, InvoiceItem, ProductStock
engine = sa.engine.create_engine('sqlite:///litedb', echo=False)
session = sessionmaker(bind=engine)()
session.query(Invoice).delete()
session.query(InvoiceItem).delete()
session.query(Product).delete()
session.query(ProductStock).delete()
session.commit()
account = session.query(Account).first()
with Timer('Insert big invoice'):
invoice = Invoice(
account=account,
invoice_number='test'
)
for _ in range(10000):
product = Product(
name=random.choice(string.ascii_uppercase) + str(random.randint(1, 10000))
)
invoice_item = InvoiceItem(
invoice=invoice,
product=product,
quantity=1,
price=1
)
session.add(invoice)
session.commit()