Skip to content

Instantly share code, notes, and snippets.

@mattupstate

mattupstate/app.py

Created Jan 22, 2014
Embed
What would you like to do?
Odd behavior regarding ordering_list and association_proxy with SQLAlchemy
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()
$ 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.