flask-admin unique-constraint on multiple columns is not working

382 views Asked by At

There is not much in the official doc- the http://flask-admin.readthedocs.org

And I found this post - How to enforce constraints in `flask-admin`? , but not much syntax there

My code

class NodeConfig(db.Model):
    id = db.Column(db.Integer, primary_key=True)

    variable = db.Column(db.String(64), nullable=False)
    value_or_representation = db.Column(db.String(64), nullable=False)

    node_id = db.Column(db.Integer(), db.ForeignKey(Node.id))
    node = db.relationship(Node, backref='config')
    db.UniqueConstraint('node_id', 'variable', name='uix_1')

    def __unicode__(self):
        return '%s:%s' % (self.variable, self.value_or_representation)

I tried with custom view as well

class NodeConfigAdmin(sqla.ModelView):
    def on_model_delete(self, model):
        db.UniqueConstraint('node_id', 'variable', name='uix_1')

    def on_model_change(self, form, model, is_created):
        db.UniqueConstraint('node_id', 'variable', name='uix_1')

    def __init__(self, session):
        # Just call parent class with predefined model.
        super(NodeConfigAdmin, self).__init__(NodeConfig, session)

But the constraint check is not triggered. I am able to create duplicate entries. Any idea?

0

There are 0 answers