sqlalchemy 踩过的坑

记录下Sqlalchemy遇到的问题,不定时更新。


 设置主键为非自增

 sqlalchemy 在sql server中默认主键是自增的,如果在数据库设置的主键不是自增的,这个时候插入就会出现异常:

 提示does not have the identity property

 这个时候需要在主键中设置autoincrement=False,显示表示非自增,才能正常写入


 不要使用in_查询一个空的可迭代对象

例如:

db.session.query(Object).filter(Object.Id.in_(ids)).delete(synchronize_session=False)

当ids为空的可迭代的对象时,就会出现如下警告,提示有性能问题

SAWarning: The IN-predicate on "xxxx.Id" was invoked with an empty sequence. This results in a contradiction, which nonetheless can be expensive to evaluate.  Consider alternative strategies for improved performance.

在创建这个语句之前做个判断,判断ids是不是为空。

 

posted @ 2017-06-18 01:09  BlackMatrix  阅读(3276)  评论(0编辑  收藏  举报