我将SQlalchemy与SQLite和aiosqLite驱动程序一起使用.

我创建了两个表,如下所示:

class AccountsModel(Base):
    __tablename__ = 'accounts'

    id: Mapped[intpk]
    email_id: Mapped[int] = mapped_column(
        ForeignKey('emails.id', ondelete='CASCADE')
    )
    new_email_id: Mapped[int | None] = mapped_column(
        ForeignKey('new_emails.id', ondelete='CASCADE'),
        nullable=True
    )
    email: Mapped['EmailsModel'] = relationship(
        back_populates='account',
        single_parent=True,
        cascade='all, delete-orphan',
        lazy='joined'
    )
    new_email: Mapped['NewEmailsModel'] = relationship(
        back_populates='account',
        single_parent=True,
        cascade='all, delete-orphan',
        lazy='joined'
    )


class EmailsModel(Base):
    __tablename__ = 'emails'

    id: Mapped[intpk]
    email: Mapped[str_255] = mapped_column(unique=True)
    account: Mapped['AccountsModel'] = relationship(
        back_populates='email',
        cascade='all, delete-orphan'
    )

发动机 :

class SessionMaker:
    def __init__(self, dsn: str):
        self._engine = create_async_engine(dsn)

    @staticmethod
    @event.listens_for(Engine, 'connect')
    def _set_sqlite_pragma(connection: Any, record: '_ConnectionRecord'):
        cursor = connection.cursor()
        cursor.execute('PRAGMA foreign_keys=ON')
        cursor.close()

    async def create_tables(self, drop=False) -> None:
        async with self._engine.begin() as conn:
            if drop:
                await conn.run_sync(Base.metadata.drop_all)
            await conn.run_sync(Base.metadata.create_all)

    @property
    def session(self) -> async_sessionmaker[AsyncSession]:
        if not hasattr(self, '_session'):
            self._session = async_sessionmaker(self._engine)
        return self._session

-你在说什么?

async def delete(
    self,
    *,
    filter_by: Dict[str, Any]
) -> None:
    async with self._session() as session:
        stmt = (
            delete(self._model)
            .filter_by(**filter_by)
        )
        await session.execute(stmt)
        await session.commit()

async def delete_by_id(
    self,
    *,
    id: int,
) -> None:
    await self.delete(filter_by=dict(id=id))

await MY_MODEL.delete_by_id(MY_ID)

当我执行上面的查询时,我会看到这个输出:

BEGIN (implicit)
DELETE FROM accounts WHERE accounts.id = ?
[cached since 20.71s ago] (3,)
COMMIT

创建声明:

CREATE TABLE accounts (
    id INTEGER NOT NULL,
    email_id INTEGER NOT NULL,
    new_email_id INTEGER,
    PRIMARY KEY (id),
    FOREIGN KEY(email_id) REFERENCES emails (id) ON DELETE CASCADE,
    FOREIGN KEY(new_email_id) REFERENCES new_emails (id) ON DELETE CASCADE
)
CREATE TABLE emails (
    id INTEGER NOT NULL,
    email VARCHAR(255) NOT NULL,
    PRIMARY KEY (id),
    UNIQUE (email)
)

我在stackover上发现了类似的问题,但仍然不明白我的代码出了什么问题.我很高兴得到任何帮助,谢谢.

推荐答案

DELETE FROM accounts WHERE accounts.id = ?

瀑布就像瀑布一样,向下流淌.

当您对FOREIGN Key进行编码时,它在LOWER表(子表)上进行编码,并指定对HIGH表(父表)的引用.因此,如果删除所引用的父邮箱/new_mails,则帐户上的编码为ON DELETE CASCADE将导致帐户行的删除.

通常,您会希望邮箱被拥有(发送/接收)给某人/某物,因此邮箱/new_mail是帐户的子项.因此,也许您应该在邮箱/new_mailes表上编码Foreign Key.然后删除帐户将CASCADE删除至邮箱/new_邮箱.

Python相关问答推荐

Pandas 除以一列中出现的每个值

如何使用矩阵在sklearn中同时对每个列执行matthews_corrcoef?

用gekko解决的ADE方程系统突然不再工作,错误消息异常:@错误:模型文件未找到.& &

如何在Python中使用io.BytesIO写入现有缓冲区?

PywinAuto在Windows 11上引发了Memory错误,但在Windows 10上未引发

Pandas 有条件轮班操作

如何过滤包含2个指定子字符串的收件箱列名?

我如何使法国在 map 中完全透明的代码?

无法使用DBFS File API路径附加到CSV In Datricks(OSError Errno 95操作不支持)

在单个对象中解析多个Python数据帧

以逻辑方式获取自己的pyproject.toml依赖项

如何强制向量中的特定元素在Gekko中处于优化解决方案中

没有内置pip模块的Python3.11--S在做什么?

每次查询的流通股数量

如何获取包含`try`外部堆栈的`__traceback__`属性的异常

Pandas:计数器的滚动和,复位

Matplotlib中的曲线箭头样式

查找数据帧的给定列中是否存在特定值

Match-Case构造中的对象可调用性测试

如何在不不断遇到ChromeDriver版本错误的情况下使用Selify?