Skip to main content
 首页 » 编程设计

python之在 Python 中插入之前的 SQLAlchemy 检查

2025年05月04日42mate10pro

我有一个关于如何检查一个条目是否已经在数据库表中的更正确/有效的方法的问题。

假设我有一个名为 Foo 的表,它具有单个属性和 Name,它也是唯一。

如果已经有同名的条目,在插入之前检查的更好方法是什么?我可以想到两种选择:

from sqlalchemy import MetaData, Table, Column, String, \ 
create_engine 
from sqlalchemy.orm import mapper, relationship, sessionmaker 
from sqlalchemy.ext.declarative import declarative_base 
from sqlalchemy.exc import IntegrityError 
 
Base = declarative_base() 
 
class Foo(Base): 
    __tablename__ = "foo" 
 
    name = Column(String(), primary_key=True) 
 
    @staticmethod 
    def silent_insert1(name): 
            if not session.query(Foo).filter(Foo.name == name).first(): 
                    foo = Foo() 
                    foo.name = name 
                    session.add(foo) 
                    session.commit() 
            else: 
                    print("already exists 1") 
 
    @staticmethod 
    def silent_insert2(name): 
            try: 
                    foo = Foo() 
                    foo.name = name 
                    session.add(foo) 
                    session.commit() 
            except(IntegrityError): 
                    print("already exists 2") 
 
 
engine = create_engine('sqlite://', echo=True) 
Base.metadata.create_all(engine) 
session = sessionmaker(engine)() 
 
Foo.silent_insert1(1) 
Foo.silent_insert2(1) 

在第一个 silent_insert1 中,当然有竞争条件。第二个似乎更防弹。

哪个更好?是否有更好的方法或推荐的方法来检查这个?

谢谢

请您参考如下方法:

你应该把session.add放在try/except

其次,这是一个哲学问题。你不会得到明确的答案。

这两种风格被称为look before you leap (LBYL),这种风格提前使用 if 语句来判断你是否应该这样做。

第二种风格被称为比许可更容易要求宽恕(EAFP),这种风格假定它会起作用并捕获并处理在规则不符合的情况下引发的异常 splinter 的。作为一般规则,python 程序员倾向于支持 EAFP,但就像任何规则一样,也有很多很多异常(exception)