Random ids in sqlalchemy (pylons)

后端 未结 3 1749
星月不相逢
星月不相逢 2021-02-09 20:28

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

3条回答
  •  情话喂你
    2021-02-09 21:04

    the best way is to use randomly generated UUIDs:

    import uuid
    
    id = uuid.uuid4()
    

    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).

提交回复
热议问题