我正在尝试根据aiomysql.sa使用aiomysql + sqlalchemy,但是 使用默认参数时出现命中问题。下面是我写的全部。
import asyncio
import sqlalchemy as sa
from aiomysql.sa import create_engine
import time
import uuid
def next_id():
return '%015d%s000' % (int(time.time() * 1000), uuid.uuid4().hex)
metadata = sa.MetaData()
Test = sa.Table('test', metadata,
sa.Column('id', sa.String(50), default=next_id, primary_key=True),
sa.Column('name', sa.String('50')),
sa.Column('created_at', sa.Float, default=time.time))
async def init_engine(loop):
print('create engine to connect sql...')
global __engine
__engine = await create_engine(
host='localhost',
port=3306,
user='zqy',
password='123456',
db='zqyblog',
loop=loop)
async def test(loop):
async with __engine.acquire() as conn:
trans = await conn.begin()
insr = Test.insert().values(name="test")
print("SQL: %r" % insr.compile().params)
await conn.execute(insr)
await trans.commit()
loop = asyncio.get_event_loop()
loop.run_until_complete(init_engine(loop))
loop.run_until_complete(test(loop))
我得到了下面的日志:
create engine to connect sql...
SQL: {'id': None, 'name': 'test', 'created_at': None}
Traceback (most recent call last):
File "db_simple.py", line 38, in test
await conn.execute(insr)
File "<my\local\path>\anaconda3\envs\python-webapp\lib\site-packages\aiomysql\sa\connection.py", line 173, in _execute
await cursor.execute(str(compiled), post_processed_params)
File "<my\local\path>\anaconda3\envs\python-webapp\lib\site-packages\aiomysql\cursors.py", line 239, in execute
await self._query(query)
File "<my\local\path>\anaconda3\envs\python-webapp\lib\site-packages\aiomysql\cursors.py", line 457, in _query
await conn.query(q)
File "<my\local\path>\anaconda3\envs\python-webapp\lib\site-packages\aiomysql\connection.py", line 428, in query
await self._read_query_result(unbuffered=unbuffered)
File "<my\local\path>\anaconda3\envs\python-webapp\lib\site-packages\aiomysql\connection.py", line 622, in _read_query_result
await result.read()
File "<my\local\path>\anaconda3\envs\python-webapp\lib\site-packages\aiomysql\connection.py", line 1105, in read
first_packet = await self.connection._read_packet()
File "<my\local\path>\anaconda3\envs\python-webapp\lib\site-packages\aiomysql\connection.py", line 593, in _read_packet
packet.check_error()
File "<my\local\path>\anaconda3\envs\python-webapp\lib\site-packages\pymysql\protocol.py", line 220, in check_error
err.raise_mysql_exception(self._data)
File "<my\local\path>\anaconda3\envs\python-webapp\lib\site-packages\pymysql\err.py", line 109, in raise_mysql_exception
raise errorclass(errno, errval)
pymysql.err.IntegrityError: (1048, "Column 'id' cannot be null")
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "db_simple.py", line 44, in <module>
loop.run_until_complete(test(loop))
File "<my\local\path>\anaconda3\envs\python-webapp\lib\asyncio\base_events.py", line 584, in run_until_complete
return future.result()
File "db_simple.py", line 39, in test
await trans.commit()
File "<my\local\path>\anaconda3\envs\python-webapp\lib\site-packages\aiomysql\utils.py", line 103, in __aexit__
await self._pool.release(self._conn)
File "<my\local\path>\anaconda3\envs\python-webapp\lib\site-packages\aiomysql\sa\engine.py", line 137, in release
raise InvalidRequestError("Cannot release a connection with "
aiomysql.sa.exc.InvalidRequestError: Cannot release a connection with not finished transaction
正如该日志第二行所述,id的值为 None ,奇怪的是,在定义列时我设置了default = next_id。有人知道吗?非常感谢。