SQLAlchemy声明式:没有任何主键的表? [英] SQLAlchemy declarative: table without any primary keys?

查看:1629
本文介绍了SQLAlchemy声明式:没有任何主键的表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用ORM创建表?没有主键的声明类?
如果我没有列任何 primary_key = True ,则无法启动。

How to create a table using ORM Declarative class without primary key? It failed to start if i didn't make any column the primary_key = True.

推荐答案

SQLAlchemy内核不会抱怨丢失主键,因此可以使用 Table(...)创建此表。但是ORM的设计需要一种方法来识别与对象相对应的行,所以不可能在ORM中使用没有主键的表。

SQLAlchemy core doesn't complain about missing primary key, so it's possible to create such table with Table(…). But ORM by design requires a way to identify the row corresponding to object, so it's not possible to use table without primary key in ORM.

为什么需要添加这个索引以后?是真正的要求还是尝试解决一些可能以其他方式解决的问题?如果您需要复合主键,可以使用 column 中的 primary_key = True 参数来定义它,或者通过在 __ table_args __ 中指定 PrimaryKeyConstraint(...)

Why do you need to add this index later? Is it real requirement or an attempt to solve some problem that probably can be solved other way? In case you need composite primary key, it's possible to define it with either primary_key=True argument in several Columns or by specifying PrimaryKeyConstraint(…) in __table_args__.

这篇关于SQLAlchemy声明式:没有任何主键的表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆