peewee-async icon indicating copy to clipboard operation
peewee-async copied to clipboard

A defect of execute insert SQL statement

Open shinegrin opened this issue 3 years ago • 0 comments

await objects.execute(TableName.insert(
    xxx
).on_conflict(
    conflict_target=[xxx],
    preserve=[xxx],
    update={xxx},
    where=(xxx)
))

if it trigger conflict and miss where caluse, it will do nothing, and will raise Exception at peewee_async.py line 600 result = row[0]

async def insert(query):
    """Perform INSERT query asynchronously. Returns last insert ID.
    This function is called by object.create for single objects only.
    """
    assert isinstance(query, peewee.Insert),\
        ("Error, trying to run insert coroutine"
         "with wrong query class %s" % str(query))

    cursor = await _execute_query_async(query)

    try:
        if query._returning:
            row = await cursor.fetchone()
            result = row[0]
        else:
            database = _query_db(query)
            last_id = await database.last_insert_id_async(cursor)
            result = last_id
    finally:
        await cursor.release()

    return result

In other words, I must write a try...except block to deal with this. But an ideal situation is that it return None but raise exception, I think.

shinegrin avatar May 24 '21 06:05 shinegrin