Reputation: 12529
I have the below db schema:
CREATE TABLE orders (
transaction_id integer primary key autoincrement,
total_price integer not null
);
CREATE TABLE order_items (
transaction_id integer REFERENCES orders(transaction_id),
SKU integer not null,
product_name text not null,
unit_price integer not null,
quantity integer not null
);
Data is sent to the db via this flask code:
@app.route('/load_ajax', methods=["GET", "POST"])
def load_ajax():
if request.method == "POST":
data = request.get_json()
for group in groupby(data, itemgetter('name')):
id, data_list = group
for d in data_list:
print d['subtotal']
db = get_db()
db.execute('insert into order_items (SKU, product_name, unit_price, quantity) values (?, ?, ?, ?)',
[d['sku'], d['name'], d['price'], d['quantity']])
db.execute('insert into orders (total_price) values (?)',
[d['subtotal']])
db.commit()
return jsonify(location=url_for('thankyou'))
A read of the orders table gives this:
1|1187.86
2|1187.86
3|1187.86
4|1187.86
5|1187.86
6|1187.86
7|102.92
8|102.92
9|102.92
10|102.92
and a read of the order_items table give this:
|ASD|Hot Sauce|10.99|1
|JKL|Chilli Peppers|8.99|1
|UIO|Sip 'n' Sizzle T-Shirt|10.5|1
|MEE|Energy Drinks|10.99|1
|FUE|Literally, Anything|1|1
|POL|Burger Ryan|1000|1
|ASD|Hot Sauce|10.99|1
|JKL|Chilli Peppers|8.99|1
|UIO|Sip 'n' Sizzle T-Shirt|10.5|1
|MEE|Energy Drinks|10.99|1
What I am trying to accomplish is generating a unique transaction ID that would be given to each item in a particular order.
First I'm not sure why transaction_id column in the order_items table is not appearing. Second, how can I get a table that might look like this:
orders:
1|1187.86
1|1187.86
1|1187.86
1|1187.86
1|1187.86
1|1187.86
2|102.92
2|102.92
2|102.92
2|102.92
order_items:
1|ASD|Hot Sauce|10.99|1
1|JKL|Chilli Peppers|8.99|1
1|UIO|Sip 'n' Sizzle T-Shirt|10.5|1
1|MEE|Energy Drinks|10.99|1
1|FUE|Literally, Anything|1|1
1|POL|Burger Ryan|1000|1
2|ASD|Hot Sauce|10.99|1
2|JKL|Chilli Peppers|8.99|1
2|UIO|Sip 'n' Sizzle T-Shirt|10.5|1
2|MEE|Energy Drinks|10.99|1
Is this something I can do sqlite?
EDIT: This is how I changed CL.'s answer to fit my code:
@app.route('/load_ajax', methods=["GET", "POST"])
def load_ajax():
if request.method == "POST":
data = request.get_json()
db = get_db()
c = db.cursor()
c.execute('insert into orders (total_price) values (?)', [data[0]['subtotal']])
transaction_id = c.lastrowid
for group in groupby(data, itemgetter('name')):
id, data_list = group
for d in data_list:
db.execute('insert into order_items (transaction_id, SKU, product_name, unit_price, quantity) values (?, ?, ?, ?, ?)',
[transaction_id, d['sku'], d['name'], d['price'], d['quantity']])
db.commit()
return jsonify(location=url_for('thankyou'))
Upvotes: 0
Views: 307
Reputation: 2641
You could use a mapping table between orders and order_item
CREATE TABLE orders (
transaction_id integer primary key autoincrement,
);
CREATE TABLE order_mapping (
id integer primary key autoincrement,
transaction_id foreign key references orders(transaction_id),
item_id foreign key references order_items(item_id)
);
CREATE TABLE order_items (
item_id integer primary key autoincrement,
SKU integer not null,
product_name text not null,
unit_price integer not null,
quantity integer not null
);
Upvotes: 0
Reputation: 180121
The transaction_id
column in the order_items
table is not appearing because you are not inserting anything into it.
You do not want to have duplicate rows in the orders
table.
Insert an order only once (I am assuming a single POST is a single order):
data = request.get_json()
subtotal = data......
c = db.cursor()
c.execute('insert into orders (total_price) values (?)', [subtotal])
transaction_id = c.lastrowid
for group in groupby(data, itemgetter('name')):
id, data_list = group
for d in data_list:
db.execute('insert into order_items (transaction_id, SKU, product_name, unit_price, quantity) values (?, ?, ?, ?)',
[transaction_id, d['sku'], d['name'], d['price'], d['quantity']])
db.commit()
Upvotes: 1