Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sqlalchemy/postgresql: database column computed default value

I have a transaction table, which has tenant_id and transaction_id columns (they make up a unique composite index). For insert operation, transaction_id must be incremented by 1, but for given tenant_id. So, using sqlalchemy framework, I manually find max transaction_id for tenant_id:

res = db.session.query(func.max(my_tran.transaction_id).label('last_id')) \
                                     .filter_by(tenant_id=tenant_id).one()
if res.last_id:
    my_tran.transaction_id = res.last_id
else:
    my_tran.transaction_id = 1

What I'd like to do instead is define the logic for my model class as server default:

class MyTran(db.Model):
    __tablename__ = 'my_tran'
    id = db.Column(db.Integer, primary_key=True)
    tenant_id = db.Column(db.Integer, db.ForeignKey('tenant.id'), nullable=False)
    transaction_id = db.Column(db.Integer, nullable=False, \
                              server_default='compute last id for tenant_id + 1')

I guess I need to create a trigger (how?), but don't know how to link to my model class.

like image 362
synergetic Avatar asked Feb 11 '26 15:02

synergetic


1 Answers

I found my answer here. Details are below (using sqlalchemy):

create_fn_my_tran_set_num = DDL(
'''
CREATE OR REPLACE FUNCTION fn_my_tran_set_num() 
RETURNS TRIGGER AS $$ 
DECLARE last_transaction_id INTEGER; 
BEGIN 
    last_transaction_id := MAX(transaction_id) FROM my_tran WHERE tenant_id = NEW.tenant_id; 
    IF last_transaction_id IS NULL THEN 
        NEW.transaction_id := 1; 
    ELSE 
        NEW.transaction_id := last_transaction_id + 1; 
    END IF; 
    RETURN NEW; 
END$$ 
LANGUAGE plpgsql 
''')
event.listen(MyTran.__table__, 'after_create', create_fn_my_tran_set_trx_id)
create_tg_my_tran_set_num = DDL(
'''
CREATE TRIGGER tg_my_tran_set_num 
BEFORE INSERT ON my_tran 
FOR EACH ROW 
EXECUTE PROCEDURE fn_my_tran_set_num(); 
''')
event.listen(MyTran.__table__, 'after_create', create_tg_my_tran_set_trx_id)
like image 50
synergetic Avatar answered Feb 13 '26 19:02

synergetic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!