我有以下数据库架构:
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
);
通过此烧录代码将数据发送到数据库:
@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'))
读取订单表会给出:
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
并且读取order_items表给出了:
|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
我想要完成的是生成一个唯一的交易ID,该ID将按特定顺序分配给每个项目。
首先,我不确定为什么order_items表中的transaction_id列没有出现。其次,我怎样才能得到一个看起来像这样的表:
订单:
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
这是我可以做sqlite的事吗?
编辑: 这就是我改变CL。的答案以适应我的代码:
@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'))
答案 0 :(得分:1)
transaction_id
表中的order_items
列未显示,因为您没有在其中插入任何内容。
您不希望orders
表中有重复的行。
仅插入一次订单(我假设单个POST是单个订单):
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()
答案 1 :(得分:0)
您可以在订单和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
);