請教一個 sqlchemy 問題,在 pycharm 下 select 方法為什麼不提示支援的 where 等關鍵詞呢

ZaZing發表於2024-04-28

程式碼如下,問題是為什麼在 select(cls) 後面 pycharm 不提示支援的關鍵詞呢。或者有對應的外掛能支援提示?

import typing
from sqlalchemy import Column, INT, String, func, event
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.sql import select
class MyBase(Base):
    # db.scalar(sql) 返回的是標量(原始資料) <models.department.Department object at 0x000002F2C2D22110>
    # db.execute(sql) 返回的是元組 (<models.department.Department object at 0x000002F2C2D22110>)
    # db.scalars(sql).all()  [<models...>, <models...>, <models...>]
    # db.execute(sql).fetchall()  [(<models...>,), (<models...>,), (<models...>,)]
    __abstract__ = True
    id = Column(INT, primary_key=True, autoincrement=True)
    create_time = Column(String(32), default=func.now())
    update_time = Column(String(32), default=func.now(), onupdate=func.now())
    # is_delete = Column(INT, default=0, comment="刪除標誌, 0 非刪除 1 刪除")

    @staticmethod
    def on_update(mapper, connection, target):
        target.update_time = now_time()

    @classmethod
    @user3n
    async def execute_by_session(cls,stmt,params,session:AsyncSession=None):
        """

        :param stmt: sqlalchemy Executable 物件
        :param params:  params 引數
        :param session:session db會話
        :return:
        """
        return await session.execute(stmt,params)

    @classmethod
    @user5n_router
    async def execute_by_local(cls,stmt,params):
        session = g.hr_db_session
        return await session.execute(stmt,params)

    @classmethod
    async def execute(cls,stmt,params:typing.Any = None):
        session = g.hr_db_session
        if session:
            return await session.execute(stmt,params)
        return cls.execute_by_session(stmt,params)


    @classmethod
    async def get_by_id(cls,id):
        conditions = [cls.id == id]
        if getattr(cls,"is_delete",None):
            conditions.append(cls.is_delete == 0)
        stmt = select(cls).where(*conditions)
        result = await cls.execute(stmt)
        data = result.scalar()
        return data

相關文章