Vishak Raj
Vishak Raj

Reputation: 171

Save value of Id in other field sqlalchemy flask

I am creating a table in database using flask sqlalchemy

In models.py,

Class User(model):
    user_id = Column(Integer, primary_key=True)
    du_id = Column(Integer)
    name = Column (NVARCHAR(20))
    #other fields...

In this table, when inserting new record, the user_id is created automatically.

How to save the user_id value in du_id field automatically when creating/inserting new record?

Thanks

Upvotes: 1

Views: 401

Answers (1)

Tim
Tim

Reputation: 3407

You can use a hybrid attribute instead of a new column

class User(model):
    user_id = Column(Integer, primary_key=True)
    name = Column (NVARCHAR(20))
    #other fields...

    @hybrid_property
    def length(self):
        return self.user_id 

Upvotes: 1

Related Questions