如何查询sqlalchemy/postgresql表格是否存在pk是uuid的对象

发布于 2025-01-30 16:01:32 字数 627 浏览 2 评论 0原文

很抱歉,如果我对Sqlalchemy的新手来说,这是不好的。假设我有以下设置:

class Student(Base):
    __tablename__ = 'student'
    id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
    name =  Column(String(50), nullable=False)

...我创建了一个新学生并将其添加到表中:

new_student = Student(name="Nick")
session.add(new_student)
session.commit()

假设主键仅是UUID,我如何检查表格是否存在>>>>的特定对象的存在。 new_student?是否可以做类似的事情:

student = session.query(Student).filter(new_student)

我在搜索中看到的所有内容都需要您使用表列,但是我表中唯一保证的唯一列是UUID,直到创建后,我才知道这是我不知道的。

感谢您的帮助!

Apologies if this is poorly phrased as I'm pretty new to SQLAlchemy. Suppose I have the following setup:

class Student(Base):
    __tablename__ = 'student'
    id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
    name =  Column(String(50), nullable=False)

...and I create a new Student and add it to the table:

new_student = Student(name="Nick")
session.add(new_student)
session.commit()

Assuming that primary key is solely the UUID, how can I check the table for the existence of the specific object referenced by new_student? Is it possible to do something like:

student = session.query(Student).filter(new_student)

Everything I've seen in my searches requires you to use a table column, but the only guaranteed unique column in my table is the UUID, which I don't know until after it's created.

Thanks for the help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文