Skip to main content
 首页 » 编程设计

python之SQLAlchemy : object. id 在括号中作为 int 返回而不是 int

2024年10月17日10freeliver54

<分区>

代码应该从 CSV 中获取通过另一种方法生成的字典 - 并生成所有必要的客户和订单实体。

   def loadOrderData(fileName,session): 
    print("Orders loading") 
    liDict = Loader.csv2liDict(fileName, {1: "date", 16: "customerName", 22: "customerPostcode", 23: "salesOrderNo", 
                                   25: "worksOrderNo", 35: "productCode", 38: "width", 39: "length", 
                                   53: "quantity"}) 
    for i in liDict: 
        customerId = -1 
        for j in session.query(Customer.id). \ 
                filter(Customer.name == i["customerName"]). \ 
                filter(Customer.postcodeDistrict == i["customerPostcode"].split(" ")[0]): 
            customerId = j 
        if customerId == -1: 
            newCustomer = Customer(name=i["customerName"], postcodeDistrict=i["customerPostcode"].split(" ")[0]) 
            session.add(newCustomer) 
            session.commit() 
            customerId = newCustomer.id 
        print("CUSTOMER ID : ",customerId) 
        newOrder = Order(date=str2date(i["date"]), customerId=customerId, salesOrderNo=i["salesOrderNo"], 
                         worksOrderNo=i["worksOrderNo"], productCode=i["productCode"], width=int(i["width"]), 
                         length=int(i["length"]), quantity=int(i["quantity"])) 
        session.add(newOrder) 
        session.commit() 

我不断收到以下错误:

 sqlalchemy.exc.InterfaceError: (raised as a result of Query-invoked autoflush; consider using a session.no_autoflush block if this flush is occurring prematurely) (sqlite3.InterfaceError) Error binding parameter 1 - probably unsupported type. [SQL: 'INSERT INTO "order" (date, "customerId", "worksOrderNo", "salesOrderNo", "productCode", width, length, quantity, assigned) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)'] [parameters: ('2016-10-26 00:00:00.000000', (1,), '', 'S/O269155', 'BKT1', 724, 1769, 0, None)] 

基本上,我推断这是由于 customerId 等于 (1,) 而不是 int。但是,我不明白为什么会发生这种情况以及如何解决它。请指教。