Discussion Forum

How to commit label(new_name)?

Here is the code that is trying to rename the entity/attribute/relation.

def schema_rename(old,new,session):
    tx_r = session.transaction().read()
    cmd = "match $x sub {}; get $x;".format(old)
    _iter = tx_r.query(cmd)
    _re = 0
    for concept in _iter:
        c = concept.map().get("x").as_remote(tx_r)
        _label = c.label()
        # print(_label)
        if _label == old:
            _old = c.label()
            _new = c.label()
            _re += 1
            print("old: {}\nnew: {}".format(_old,_new))
        if c.is_entity_type():
            ### role
            role_old = "r_{}".format(old)
            role_new = "r_{}".format(new)
            cmd_role = "match $x sub {}; get $x;".format(role_old)
            role_iter = tx_r.query(cmd_role)
            if role_iter:
                for role_concept in role_iter:
                    role_c = role_concept.map().get("x").as_remote(tx_r)
                    role_label = role_c.label()
                    # print(role_label)
                    if role_label == role_old:
                        _role_old = role_c.label()
                        _role_new = role_c.label()
                        _re += 1
                        print("role_old: {}\nrole_new: {}".format(_role_old,_role_new))
            ### relation
            cmd_relation = "match $x sub relation; get $x;"
            relation_iter = tx_r.query(cmd_relation)
            if relation_iter:
                for relation_concept in relation_iter:
                    relation_c = relation_concept.map().get("x").as_remote(tx_r)
                    relation_label = relation_c.label()
                    # print(relation_label)
                    if old in relation_label:
                        relation_new = relation_label.replace(old,new)
                        _relation_old = relation_c.label()
                        _relation_new = relation_c.label()
                        _re += 1
                        print("relation_old: {}\nrelation_new: {}".format(_relation_old,_relation_new))
    return _re

Since it’s using read() transaction, not write() transaction, there is no commit. What I found is that, if I put tx_r inside of the def, I couldn’t pass the change to tx_r outside of the def.

How to deal with those kind of problem?


you can just change this line to:

    tx_r = session.transaction().write()

and then you should be able to commit!