'Flask-sqlalchemy, good practices for generating unique key from row?

I want to create a table where the first column will have a unique key based on 2-5 columns. What is the best way for me to do this?

enter image description here



Solution 1:[1]

You don't need to create extra column for that. Dependend how you create your models, you can use composite primary key:

class Model(Base):
    __tablename__ = "model"

    article = Column(Integer, primary_key=True)
    telegramID = Column(Integer, primary_key=True)    
    type_settings = Column(String, primary_key=True)
    ...

    @property
    def composite_key(self):
        return (self.article, self.telegramID, self.type_settings, ...)

Now to get the specific row for given key you can use simple query

row = db.query(Model).get((article, telegramID, type_settings, ...))
print(row.composite_key)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1