简体   繁体   English

在Python中生成用户ID

[英]Generating User IDs in Python

I've created a simple DB using SQLAlchemy in python and there is a column for a unique user ID for each user entered. 我已经在python中使用SQLAlchemy创建了一个简单的数据库,并且为输入的每个用户提供了一个唯一用户ID的列。 I want to have a running counter that supplies the next ID whenever a new User is added. 我希望有一个运行计数器,每当添加新用户时,该计数器便会提供下一个ID。

What would be the best way to implement this? 实施此方法的最佳方法是什么? A column with the value of the last id number, or would that be an entirely separate table (seems wasteful.) Is there a way for it to check the last User.id and add one? 具有最后一个id号的值的列,或者那将是一个完全独立的表(似乎很浪费。)是否有办法检查最后一个User.id并添加一个? Here is the User base class: 这是用户基类:

class User(Base):
    __tablename__ = 'users'

    id = Column(Integer, primary_key = True)
    email_address = Column(String, nullable = False)
    username = Column(String, nullable = False)
    password = Column(String, nullable = False)
    first_name = Column(String)
    full_name = Column(String)
    join_date = Column(DateTime, default=func.now()) # enters the time upon initialization

    def __init__(self, email_address, username, password):
        self.email_address = email_address
        self.username = username
        self.password = password

    def __repr__(self):
        return "<User('%s', '%s', '%s', '%s')>" % (self.email_address, self.username, self.password, self.join_date)

Read this: http://www.sqlalchemy.org/docs/core/schema.html#defining-sequences . 阅读以下内容: http : //www.sqlalchemy.org/docs/core/schema.html#defining-sequences

Key generation is a part of the database. 密钥生成是数据库的一部分。

You simply want the database to assign unique keys. 您只希望数据库分配唯一键。

What database are you using? 您正在使用什么数据库? If it's MySQL you might be done already. 如果是MySQL,您可能已经完成了。 Try creating a User where User.id = None. 尝试创建一个用户,其中User.id = None。 It should take the next available id. 它应该带有下一个可用的ID。 If it is autoincrementing, after inserting the User the correct id will be placed in the User.id. 如果它是自动递增的,则在插入用户后,正确的ID将放置在User.id中。

I normally create my schema outside of SQLAlchemy. 我通常在SQLAlchemy之外创建我的架构。 What you want is for the id column to be the primary key, not null, and set to autoincrement. 您想要的是id列是主键,而不是null,并设置为autoincrement。 Then, if you don't assign an id it will take the next id automatically. 然后,如果您不分配ID,它将自动获取下一个ID。

id will take unique value automatically after flushing newly created object to DB, so, as I know, you must run session.flush() if you need user id right now: 将新创建的对象刷新到数据库后,id将自动获得唯一值,因此,据我所知,如果现在需要用户ID,则必须运行session.flush():

>>> user = User(email_address, username, password)
>>> print user.id
None
>>> session.add(user)
>>> session.flush()
>>> print user.id
100500

Also, please, don't use this terrible __init__ methods! 另外,请不要使用这种可怕的__init__方法! You need a custom init only if you want to add a some custom logic or validation on it. 仅当您要在其上添加一些自定义逻辑或验证时,才需要自定义init。 But such simple cases as assigning keyword arguments as corresponding object attributes already provided by declarative_base class (Base). 但是,诸如将关键字参数分配为declarative_base类(Base)已提供的相应对象属性的简单情况。

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

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