librelist archives

« back to archive

having many to many model trouble

having many to many model trouble

From:
Cord Macleod
Date:
2013-01-18 @ 04:51
Here is a configuration I am attempting to setup.  It is supposed to have a
list of users and locations.  A location may have many users and of course
a user may have many locations.  A user may also have a unique name for
each location.

My attempt hasn't been working unfortunately, can anyone provide a bit of
guidance?

Here is the code:

user_locations = db.Table('user_locations',
    db.Column('user_id', db.Integer, db.ForeignKey('user.id')),
    db.Column('location_id', db.Integer, db.ForeignKey('location.id')),
    db.Column('description', db.String(128)),
    db.Column('created_time', db.DateTime),
    db.Index('user_id_index', 'user_id'),
    db.Index('location_id_index', 'location_id'),
    db.UniqueConstraint('user_id', 'location_id', name='user_location')
)

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(128), unique=True)
    email = db.Column(db.String(128), unique=True)
    password = db.Column(db.String(128))
    user_locations = db.relationship('user_locations',
secondary=user_locations,backref=db.backref('user', lazy='dynamic'))

    def __init__(self, username, email, password):
        self.username = username
        self.email = email
        self.password = password

    def __repr__(self):
        return '<User %r>' % self.username

class Location(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    address = db.Column(db.String(128), unique=True)
    lat = db.Column(db.String(32), unique=True)
    lng = db.Column(db.String(32), unique=True)

    def __init__(self, address, lat, lng):
        self.address = address
        self.lat = lat
        self.lng = lng

    def __repr__(self):
        return '<Address: %r>' % self.address

This is one the errors I get when attempting to add a user (although there
are more when playing around with the db.relationship method):

ArgumentError: Class object expected, got 'Table('user_locations',
MetaData(bind=None), Column('user_id', Integer(), ForeignKey('user.id'),
table=<user_locations>), Column('location_id', Integer(), ForeignKey('
location.id'), table=<user_locations>), Column('description',
String(length=128), table=<user_locations>), Column('created_time',
DateTime(), table=<user_locations>), schema=None)'.

Re: [flask] having many to many model trouble

From:
anh le
Date:
2013-01-18 @ 06:13
On Fri, Jan 18, 2013 at 11:51 AM, Cord Macleod <cordmacleod@gmail.com> wrote:
> Here is a configuration I am attempting to setup.  It is supposed to have a
> list of users and locations.  A location may have many users and of course a
> user may have many locations.  A user may also have a unique name for each
> location.
>
> My attempt hasn't been working unfortunately, can anyone provide a bit of
> guidance?
>
> Here is the code:
>
> user_locations = db.Table('user_locations',
>     db.Column('user_id', db.Integer, db.ForeignKey('user.id')),
>     db.Column('location_id', db.Integer, db.ForeignKey('location.id')),
>     db.Column('description', db.String(128)),
>     db.Column('created_time', db.DateTime),
>     db.Index('user_id_index', 'user_id'),
>     db.Index('location_id_index', 'location_id'),
>     db.UniqueConstraint('user_id', 'location_id', name='user_location')
> )

Your association table (user_locations) has many columns other than
just foreign keys
you should use Association Object instead:
http://docs.sqlalchemy.org/en/latest/orm/relationships.html#association-object

If you remove all none foreign key columns from user_locations try

user_locations =
db.relationship('Location',secondary=user_locations,backref=db.backref('user',
lazy='dynamic'))


>
> class User(db.Model):
>     id = db.Column(db.Integer, primary_key=True)
>     username = db.Column(db.String(128), unique=True)
>     email = db.Column(db.String(128), unique=True)
>     password = db.Column(db.String(128))
>     user_locations = db.relationship('user_locations',
> secondary=user_locations,backref=db.backref('user', lazy='dynamic'))
>
>     def __init__(self, username, email, password):
>         self.username = username
>         self.email = email
>         self.password = password
>
>     def __repr__(self):
>         return '<User %r>' % self.username
>
> class Location(db.Model):
>     id = db.Column(db.Integer, primary_key=True)
>     address = db.Column(db.String(128), unique=True)
>     lat = db.Column(db.String(32), unique=True)
>     lng = db.Column(db.String(32), unique=True)
>
>     def __init__(self, address, lat, lng):
>         self.address = address
>         self.lat = lat
>         self.lng = lng
>
>     def __repr__(self):
>         return '<Address: %r>' % self.address
>
> This is one the errors I get when attempting to add a user (although there
> are more when playing around with the db.relationship method):
>
> ArgumentError: Class object expected, got 'Table('user_locations',
> MetaData(bind=None), Column('user_id', Integer(), ForeignKey('user.id'),
> table=<user_locations>), Column('location_id', Integer(),
> ForeignKey('location.id'), table=<user_locations>), Column('description',
> String(length=128), table=<user_locations>), Column('created_time',
> DateTime(), table=<user_locations>), schema=None)'.
>

Re: [flask] having many to many model trouble

From:
Cord Macleod
Date:
2013-01-19 @ 05:53
On Thu, Jan 17, 2013 at 10:13 PM, anh le <anhlh2@gmail.com> wrote:

> Your association table (user_locations) has many columns other than
> just foreign keys
> you should use Association Object instead:
>
> http://docs.sqlalchemy.org/en/latest/orm/relationships.html#association-object
>
> If you remove all none foreign key columns from user_locations try
>
> user_locations =
>
> db.relationship('Location',secondary=user_locations,backref=db.backref('user',
> lazy='dynamic'))
>

 Exactly what I was after.  Thank you.