如何查询sqlalchemy/postgresql表格是否存在pk是uuid的对象
很抱歉,如果我对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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论