Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly use association_proxy and ordering_list together with SQLAlchemy

Based on some posts on the SQLAlchemy Google Group:

https://groups.google.com/forum/#!topic/sqlalchemy/S4_8PeRBNJw https://groups.google.com/forum/#!topic/sqlalchemy/YRyI7ic1QkY

I assumed I could successfully use the assocation_proxy and ordering_list extensions to create an ordered, many to many relationship between two models such as in the following Flask/SQLAlchemy code:

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.ext.associationproxy import association_proxy
from sqlalchemy.ext.orderinglist import ordering_list

app = Flask(__name__)
app.debug = True
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://'
db = SQLAlchemy(app)


class Image(db.Model):
    __tablename__ = 'images'

    id = db.Column(db.Integer, primary_key=True)
    filename = db.Column(db.String(255))


class UserImage(db.Model):
    __tablename__ = 'user_images'

    user_id = db.Column(db.Integer, db.ForeignKey('users.id'), primary_key=True)
    image_id = db.Column(db.Integer, db.ForeignKey('images.id'), primary_key=True)
    image = db.relationship('Image')
    position = db.Column(db.Integer)


class User(db.Model):
    __tablename__ = "users"

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(255))

    _images = db.relationship('UserImage',
        order_by='UserImage.position',
        collection_class=ordering_list('position'))
    images = association_proxy('_images', 'image')


with app.app_context():
    db.create_all()
    user = User(name='Matt')
    user.images.append(Image(filename='avatar.png'))
    db.session.add(user)
    db.session.commit()

if __name__ == '__main__':
    app.run()

But I end up with the following traceback:

$ python app.py
Traceback (most recent call last):
  File "app.py", line 44, in <module>
    user.images.append(Image(filename='avatar.png'))
  File "/Users/matt/.virtualenvs/sqlalchemy-temp/lib/python2.7/site-packages/sqlalchemy/ext/associationproxy.py", line 595, in append
    item = self._create(value)
  File "/Users/matt/.virtualenvs/sqlalchemy-temp/lib/python2.7/site-packages/sqlalchemy/ext/associationproxy.py", line 522, in _create
    return self.creator(value)
TypeError: __init__() takes exactly 1 argument (2 given)

Is this not possible or am I doing something blatantly wrong?

like image 975
Matt W Avatar asked Jan 22 '14 19:01

Matt W


1 Answers

sooo close ;-)

Please read Creation of New Values secion of association_proxy extension.
In order to make your code work, you can either

Option-1: add the following constructor to UserImage class:

def __init__(self, image):
    self.image = image

or

Option-2: override the default creator with your function:

images = association_proxy('_images', 'image',
    creator=lambda _i: UserImage(image=_i),
    )

I personally favor the latter.

like image 199
van Avatar answered Oct 14 '22 04:10

van