DEV Community

Varun Hegde
Varun Hegde

Posted on

Getting Error in SqlAlchemy

I was trying to define one to many relationship in sqlalchemy in flask.
This are my model.

class Storm(db.Model):
    __tablename__='storm'
    __table_args__ (
        db.UniqueConstraint('name','year'),
    )
    id=db.Column(db.Integer,primary_key=True)
    name=db.Column(db.String(50),nullable=False)
    year=db.Column(db.Integer,nullable=False)
    cat=db.Column(db.String(50))
    pos=db.realationship('Position',backref='storm_info',lazy=True)

    def __repr__(self):
        return '<storm_name : {},storm_year : {}>'.format(self.name,self.year)

class Position(db.Model):
    lat=db.Column(db.Float)
    longi=db.Column(db.Float)
    storm_id=db.Column(db.Integer,db.ForeignKey('storm.id'),nullable=False)
    pressure-db.Column(db.Float)

I want compound uniqueness for name and year.
but i am getting this error -
NameError: name 'table_args' is not defined.
can anyone please suggest what is the problem and how i can define it ?
and whats the difference between this and defining by declarative model?
Thanks in advance

Top comments (2)

Collapse
 
burdier profile image
Burdier • Edited

class Storm(db.Model):
tablename='storm'
table_args (
db.UniqueConstraint('name','year'),
)

You are actually calling a magic method called tableargs__ but at this point there is not a function called like that, so I think what you want to do is something like this:

class Storm(db.Model):
tablename='storm'
table_args = ( #new edit
db.UniqueConstraint('name','year'),
)

Collapse
 
varuntumbe profile image
Varun Hegde

Ohh man, am so embarrassed right now. Thanks i got the mistake