我将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上发现了类似的问题,但仍然不明白我的代码出了什么问题.我很高兴得到任何帮助,谢谢.