sqlalchemy中的随机ID(定向塔) [英] Random ids in sqlalchemy (pylons)

查看:284
本文介绍了sqlalchemy中的随机ID(定向塔)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用铁塔和sqlalchemy,我想知道如何将一些随机ID用作primary_key.

I'm using pylons and sqlalchemy and I was wondering how I could have some randoms ids as primary_key.

推荐答案

最好的方法是使用随机生成的UUID:

the best way is to use randomly generated UUIDs:

import uuid

id = uuid.uuid4()

uuid数据类型在某些数据库(例如Postgresql)中是本地可用的(为此,SQLAlchemy具有本机PG uuid数据类型-在0.5中称为sqlalchemy.databases.postgres.PGUuid).您还应该能够将uuid存储在任何16字节的CHAR字段中(尽管我还没有在MySQL或其他版本上专门尝试过此操作).

uuid datatypes are available natively in some databases such as Postgresql (SQLAlchemy has a native PG uuid datatype for this purpose - in 0.5 its called sqlalchemy.databases.postgres.PGUuid). You should also be able to store a uuid in any 16 byte CHAR field (though I haven't tried this specifically on MySQL or others).

这篇关于sqlalchemy中的随机ID(定向塔)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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