Random ids in sqlalchemy (pylons)

后端 未结 3 1743
星月不相逢
星月不相逢 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:06

    Or with ORM mapping:

    import uuid
    from sqlalchemy import Column, Integer, String, Boolean
    
    def uuid_gen():
        return str(uuid.uuid4())
    
    Base = declarative_base()
    class Device(Base):
        id = Column(String, primary_key=True, default=uuid_gen)
    

    This stores it as a string providing better database compatibility. However, you lose the database's ability to more optimally store and use the uuid.

提交回复
热议问题