librelist archives

« back to archive

UniqueConstraint with flask-sqlalchemy

UniqueConstraint with flask-sqlalchemy

From:
Wael Orabi
Date:
2012-09-18 @ 16:04
Hello,

I'm trying to use UniqueConstaint

http://docs.sqlalchemy.org/en/latest/core/schema.html#sqlalchemy.schema.UniqueConstraintwith
flask-sqlalchemy to create a composite unique key, but I'm not sure
where it should go in the model definition, because just adding it does not
do anything, I also tried adding it in __init__ with the same effect.
anyone tried that before?
--w

Re: [flask] UniqueConstraint with flask-sqlalchemy

From:
Mark Steve Samson
Date:
2012-09-18 @ 16:39
Is it also your primary key? You can use primary_key=True for both fields 
and it'll have the  same effect. 

-- 
Steve


On Wednesday, September 19, 2012 at 12:04 AM, Wael Orabi wrote:

> Hello,
> 
> I'm trying to use UniqueConstaint 
http://docs.sqlalchemy.org/en/latest/core/schema.html#sqlalchemy.schema.UniqueConstraint
with flask-sqlalchemy to create a composite unique key, but I'm not sure 
where it should go in the model definition, because just adding it does 
not do anything, I also tried adding it in __init__ with the same effect. 
anyone tried that before?
> --w

Re: [flask] UniqueConstraint with flask-sqlalchemy

From:
anh le
Date:
2012-09-19 @ 10:20
Hi ,


On Tue, Sep 18, 2012 at 11:04 PM, Wael Orabi <wael3rabi@gmail.com> wrote:
> Hello,
>
> I'm trying to use UniqueConstaint
> 
http://docs.sqlalchemy.org/en/latest/core/schema.html#sqlalchemy.schema.UniqueConstraint
> with flask-sqlalchemy to create a composite unique key, but I'm not sure
> where it should go in the model definition, because just adding it does not
> do anything, I also tried adding it in __init__ with the same effect. anyone
> tried that before?
> --w
>

Have you tried something like this:

class Test(db.Model):
    __tablename__ = 'test'
    __table_args__ = ( db.UniqueConstrain('columnA', 'columnB'), { } )
    columnA = db.Column(db.String(100))
    columnB = db.Column(db.String(100))
....

it works for me.