AttributeError:“会话”对象没有属性“会话”

Posted

技术标签:

【中文标题】AttributeError:“会话”对象没有属性“会话”【英文标题】:AttributeError: 'Session' object has no attribute 'session' 【发布时间】:2022-01-16 04:31:20 【问题描述】:

我正在创建一个端点,它将使用带有 python 的 fastApi 实现反转。我有一个端点,它通过 post 请求接收一个 id 并在事务模型上调用一个反向函数。但我收到错误 AttributeError: 'Session' object has no attribute 'session' on 'db.session.add(transaction)' 。我不确定这是从哪里来的。

交易模型

class Transaction(Base):
    __tablename__ = "transactions"
    id = Column(Integer, primary_key=True, index=True)
    uuid = Column(UUID(as_uuid=True), nullable=False, default=uuid.uuid4)
    amount = Column(Numeric(18, 2))
    type = Column(
        Enum(TransactionTypes, name="transaction_trans_types"), nullable=False
    )
    wallet_type = Column(
        Enum(WalletTypes, name="transaction_wallet_types"), nullable=False
    )
    created_when = Column(DateTime(timezone=True), server_default=func.now())
    payment_ref = Column(String, nullable=False)
    running_balance = Column(Numeric(18, 2))
    currency = Column(String, default="KES")
    notes = Column(String)
    paid_by = Column(UUID(as_uuid=True), ForeignKey("patients.uuid"), nullable=True,)
    patient_uuid = Column(UUID(as_uuid=True), ForeignKey("patients.uuid"))
    wallet_uuid = Column(UUID(as_uuid=True), ForeignKey("wallets.uuid"))
    paid_by_names = Column(String)

    def reverse(self,db: Session,notes:str =None,):
        transaction =Transaction(amount =self.amount,type= self.type,wallet_type = self.wallet_type,payment_ref = self.payment_ref,
        notes= notes )
        
        db.session.add(transaction)
        db.session.commit()
        db.session.refresh(transaction)

        reversal = Reversal(transaction_uuid=transaction.uuid,reversed_transaction_uuid= self.reversed_transaction_uuid)
        db.session.add(reversal)
        db.session.commit()
        db.session.refresh(reversal)

        return transaction

反转模型

class Reversal(Base):
    __tablename__ = "reversals"  
    id = Column(Integer, primary_key=True, index=True)
    transaction_uuid = Column(UUID(as_uuid=True), ForeignKey("transactions.uuid"))
    reversed_transaction_uuid = Column(UUID(as_uuid=True),ForeignKey("transactions.uuid"))

    __table_args__ = (
        UniqueConstraint("reversed_transaction_uuid"),
    )

端点

@router.post("/transaction_id/reverse",)   
async def reverse_transaction(
    *,
    db: Session = Depends(deps.get_db),
    transaction_id: int,
    current_user: models.User = Depends(deps.get_current_active_user),
) -> Any:
    """
    Reverse transaction
    """ 
    if not current_user.is_superuser:
        raise HTTPException(
            status_code=status.HTTP_403_FORBIDDEN,
            detail="Not allowed to reverse transactions",
        )
    try:
        transaction = (
            db.query(Transaction).filter(Transaction.id == transaction_id).scalar()
        )
   
        transaction.reverse(db,transaction)
    except NoResultFound:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND, detail="Not found",
        )    

    return transaction    

这是我的 deps.db

def get_db() -> Generator:
    try:
        db = SessionLocal()
        yield db
    finally:
        db.close()

【问题讨论】:

【参考方案1】:

最重要的部分是看看deps.get_db() 的样子。

但我猜根据你的类型提示,这已经给了你一个 Session。因此,在您的 reverse 方法中不要使用 db.session,而只需使用 db

【讨论】:

我已经添加了我的 deps.db

以上是关于AttributeError:“会话”对象没有属性“会话”的主要内容,如果未能解决你的问题,请参考以下文章

AttributeError:模块“张量流”没有属性“会话”

AttributeError: 'RDD' 对象没有属性 'show'

AttributeError:“NumpyArrayIterator”对象没有属性“类”

AttributeError:'list' 对象没有属性 'size'

AttributeError:“模块”对象没有属性“WebSocketApp”

AttributeError: 'float' 对象没有属性 'split'