The code is supposed to take a dictionary produced by another method from a CSV - and produce all the necessary customer and order entities.
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()
I keep getting the following error:
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)]
Basically, I've deduced it to be due to customerId equalling (1,) instead of being an int. I don't understand why this happening and how to fix it, however. Advice, please.