I've created a simple DB using SQLAlchemy in python and there is a column for a unique user ID for each user entered. I want to have a running counter that supplies the next ID whenever a new User is added.
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? 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.
Key generation is a part of the database.
You simply want the database to assign unique keys.