【问题标题】:Duplicate entry '12' for key 'order_details.PRIMARY'键“order_details.PRIMARY”的重复条目“12”
【发布时间】:2023-04-03 07:18:03
【问题描述】:

当我尝试使用 python 和 SQL 查询添加新订单及其详细信息时,我收到此错误消息 mysql.connector.errors.IntegrityError: 1062 (23000): Duplicate entry '13' for key 'order_details.PRIMARY'。 完整的错误信息:

这是我的代码:

from datetime import datetime
from sql_connection import get_sql_connection

def insert_order(connection, order):
    cursor = connection.cursor()

    order_query = ("INSERT INTO orders "
             "(customer_name, total, datetime)"
             "VALUES (%s, %s, %s)")
    order_data = (order['customer_name'], order['grand_total'], datetime.now())

    cursor.execute(order_query, order_data)
    order_id = cursor.lastrowid

    order_details_query = ("INSERT INTO order_details "
                           "(order_id, product_id, quantity, total_price)"
                           "VALUES (%s, %s, %s, %s)")

    order_details_data = []
    for order_detail_record in order['order_details']:
        order_details_data.append([
            order_id,
            int(order_detail_record['product_id']),
            float(order_detail_record['quantity']),
            float(order_detail_record['total_price'])
        ])
    cursor.executemany(order_details_query, order_details_data)

    connection.commit()

    return order_id

if __name__ == '__main__':
    connection = get_sql_connection()
    print(insert_order(connection, {
        'customer_name': 'dhaval',
        'grand_total': '500',
        'order_details': [
            {
                'product_id': 3,
                'quantity': 2,
                'total_price': 50
            },
            {
                'product_id': 4,
                'quantity': 1,
                'total_price': 30
            }
        ]
    }))

订单

order_details

order_details 外键

【问题讨论】:

  • order_details中主键只有order_id所以不能复制,这是正常的

标签: python mysql sql


【解决方案1】:

报错正常如表order_details,主键只有order_id

您为插入的每一行提供两倍相同的order_id

for order_detail_record in order['order_details']:
    order_details_data.append([
        order_id, # <<  <<  <<  <<  <<  <<  <<  <<  <<  <<  <<  
        int(order_detail_record['product_id']),
        float(order_detail_record['quantity']),
        float(order_detail_record['total_price'])
    ])

关于您的代码和数据库,我会说表 order_details 应该同时具有 order_idproduct_id 作为主键

【讨论】:

  • 如果我的订单中有多个商品,那么每个商品的 order_id 应该相同。
  • 我希望订单中每件商品的 order_id 都相同
  • 你能举个例子吗?我对 SQL 很陌生。
  • @RishiPurwar 是什么?如何创建你的表?在您的屏幕截图中,不仅 order_id 还应在 PK 下检查 product_id
【解决方案2】:

更改订单详情 到

CREATE TABLE order_Details
(od_id BIGINT AutO_INCREMENT PRIMARY KEY,
order_id int,
product_id int,
qualntitiy double,
INDEX (order_id),
total_price Double,
    FOREIGN KEY (order_id)
        REFERENCES `orders`(order_id)
        ON DELETE CASCADE
        ON UPDATE CASCADE
        );

这将允许许多 orde_ids

但是

  1. order_id 应该是 bigint int 太小了
  2. tota_price 应为 DECIMAL(10,2)

【讨论】:

    猜你喜欢
    • 2017-02-17
    • 2014-12-30
    • 2013-05-08
    • 2021-09-17
    • 2012-04-05
    • 2011-11-11
    • 2012-07-23
    • 2013-09-30
    相关资源
    最近更新 更多