简体   繁体   English

python webframeworks中的序列/身份支持

[英]Sequence / Identity support in python webframeworks

Currently I'm evaluating web frameworks with an ORM layer and I've stumbled upon an interesting issue. 目前,我正在评估带有ORM层的Web框架,但偶然发现了一个有趣的问题。 I used tables with an ID column in a Java EE (EJB3/JPA) application with different databases. 我在具有不同数据库的Java EE(EJB3 / JPA)应用程序中使用带有ID列的表。 In SAPDB I could define a sequence and use the jpa sequence generator annotation to deal with it, the same way I did on an oracle database previously. 在SAPDB中,我可以定义一个序列并使用jpa序列生成器批注来处理它,就像我以前在oracle数据库上所做的一样。 When I switched to SQL Server 2005 I suddenly had to replace the whole thing to an IDENTITY annotation because thats how SQL Server handles id generation apparently. 当我切换到SQL Server 2005时,突然不得不将整个内容替换为一个IDENTITY批注,因为那显然是SQL Server处理ID生成的方式。 I was kinda disappointed the JPA didn't give me an abstraction above that and I guess its not the only limitation I will come across using different databases. 我有点失望,JPA没有给我一个以上的抽象,我想它不是我使用不同数据库时遇到的唯一限制。

Now to my question: I've read that in web2py for example all tables must be altered to use an auto_increment index. 现在我的问题是:例如,我已经在web2py中阅读了所有表,必须对其进行更改以使用auto_increment索引。 Does it support sequence generators or identity columns as well? 它是否也支持序列生成器或标识列? How about the other web frameworks? 其他网络框架呢? Will they allow me to port my application across multiple legacy databases? 他们会允许我在多个旧数据库之间移植我的应用程序吗? That is to say, the table definitions are the same except for the identity column and the slight differences between data type definition for Strings (I remember altering the create statements there but I cant remember what exactly was necessary) 也就是说,除了identity列和String的数据类型定义之间的细微差别之外,表定义是相同的(我记得在那里更改了create语句,但我不记得确切需要什么)

There's also a difference in obtaining the ID - with the sequence generator you can access the generated ID before the commit, with auto_increment in MySQL, afaik, you can't. 获取ID也是有区别的-使用序列生成器,您可以在提交之前访问生成的ID,而使用MySQL中的auto_increment,afaik则不能。

SQLAlchemy can handle both ways just fine. SQLAlchemy可以很好地处理两种方式。 Given a schema declaration like this: 给定这样的模式声明:

class Foo(Base):
    id = Column(Integer, Sequence('foo_seq', optional=True), primary_key=True)
    ...

SQLAlchemy will use the built in auto-incrementing paradigm of the database if it has one, otherwise it will define a sequence. SQLAlchemy将使用数据库的内置自动递增范例(如果有的话),否则将定义一个序列。 It also manages the fetching of the generated id, but as the API is least common denominator you obviously can't access it before the insert. 它还管理对生成的id的提取,但是由于API是最不常用的分母,因此您显然无法在插入之前访问它。 You can ofcourse fetch the id manually if you want to. 当然,您可以根据需要手动获取ID。 For example for PostgreSQL that has sequences, but also has an auto-incrementing serial datatype that's backed by sequences, this will use the serial datatype. 例如,对于既具有序列又具有由序列支持的自动递增serial数据类型的PostgreSQL,它将使用serial数据类型。 With older versions it will automatically prefetch the next sequence value to use in the insert, with SQLAlchemy 0.6 series and Postgres 8.3+ it will use the INSERT ... RETURNING ... feature to insert and fetch in one go. 对于较旧的版本,它将自动预取下一个要在插入中使用的序列值,对于SQLAlchemy 0.6系列和Postgres 8.3+,它将使用INSERT ... RETURNING ...功能一次性插入和提取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM