librelist archives

« back to archive

SQLAlchemy add method for many-to-many relationship

SQLAlchemy add method for many-to-many relationship

From:
Matthieu Bosquet
Date:
2011-11-12 @ 03:35
Hi,
Could someone please point me to an example of add method for a
many-to-many relationship using SQLAlchemy ?
@Armin : It would be very nice to have such an example in the FlaskSQLAlchemy

documentation<http://packages.python.org/Flask-SQLAlchemy/queries.html#inserting-records>

In the best of the world, we would even have a Flaskr2 demo app :
  - It would have 2 extra tables Tag and Tags
  - It would use SQLAlchemy
  - It would show a clever use of JQuery to autocomplete the tags in the UI

I'd be happy to help making that happen.

Cheers,
Matthieu

Re: [flask] SQLAlchemy add method for many-to-many relationship

From:
Adam Patterson
Date:
2011-11-12 @ 04:27
http://www.sqlalchemy.org/docs/orm/relationships.html#many-to-many

Just put the method you want on one of the models. You can't put a
method on a relationship.

On Sat, Nov 12, 2011 at 10:35 AM, Matthieu Bosquet
<matthieubosquet@gmail.com> wrote:
> Hi,
> Could someone please point me to an example of add method for a many-to-many
> relationship using SQLAlchemy ?
> @Armin : It would be very nice to have such an example in the
> FlaskSQLAlchemy documentation
>
> In the best of the world, we would even have a Flaskr2 demo app :
>   - It would have 2 extra tables Tag and Tags
>   - It would use SQLAlchemy
>   - It would show a clever use of JQuery to autocomplete the tags in the UI
>
> I'd be happy to help making that happen.
>
> Cheers,
> Matthieu
>

Re: [flask] SQLAlchemy add method for many-to-many relationship

From:
Matthieu Bosquet
Date:
2011-11-12 @ 20:43
Wow, I finally figured it out.
So, my problem is that I was trying to commit values and not an object.

So here would be a basic many-to-many test.py app :

-------------------------------------------------------------------------------------------------------------
from flask import Flask
from flaskext.sqlalchemy import SQLAlchemy

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

tags = db.Table('tags',
    db.Column('tag_id', db.Integer, db.ForeignKey('tag.id')),
    db.Column('page_id', db.Integer, db.ForeignKey('page.id'))
)

class Page(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(120))
    tags = db.relationship('Tag', secondary=tags,
backref=db.backref('pages', lazy='dynamic'))

    def __init__(self, title, tags):
        self.title = title
        self.tags = tags

    def __repr__(self):
        return '<Page %r, Tags %r>' % (self.title, self.tags)

class Tag(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(120))

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

    def __repr__(self):
        return '<Tag %r>' % self.name

-------------------------------------------------------------------------------------------------------------

Then from the python command line :

-------------------------------------------------------------------------------------------------------------
# Import and create

>>> from test import db
>>> db.create_all()
>>> from test import Page, Tag

# Insert a Page with a tag let's say 'youpi'

>>> tag = Tag.query.filter_by(name='youpi').first()
>>> if tag is None:
. . .    tag = Tag('youpi')
. . .
>>> page = Page('Page with a youpi tag', [tag])
>>> db.session.add(page)
>>> db.session.commit()
>>> print Page.query.all()
[<Page u'Page with a youpi tag', Tags [<Tag u'youpi'>]>]
>>> print Tag.query.all()
[<Tag u'youpi'>]

# Insert a Page with no tag

>>> page = Page('Page with no tag', [None])
>>> db.session.add(page)
>>> db.session.commit()
>>> print Page.query.all()
[<Page u'Page with a youpi tag', Tags [<Tag u'youpi'>]>, <Page u'Page with
no tag', Tags []>]

# Insert a Page with multiple tags

>>> tags = [Tag('fantastic'), Tag('multiple'), Tag('tags')]
>>> page = Page('Page with multiple tags', tags)
>>> db.session.add(page)
>>> db.session.commit()
>>> print Page.query.all()
[<Page u'Page with a youpi tag', Tags [<Tag u'youpi'>]>, <Page u'Page with
no tag', Tags []>, <Page u'Page with multiple tags', Tags [<Tag
u'fantastic'>, <Tag u'multiple'>, <Tag u'tags'>]>]

-------------------------------------------------------------------------------------------------------------

But really I'm not sure I actually did figure out the best way to do it.

@Armin : Could you please validate my method and tell me what would be the
best way to add it to the documentation ?

I'm sure it should be helpfull to a few newbies like me ^^,

Cheers,
Matthieu


On 12 November 2011 05:27, Adam Patterson <fakeempire@gmail.com> wrote:

> http://www.sqlalchemy.org/docs/orm/relationships.html#many-to-many
>
> Just put the method you want on one of the models. You can't put a
> method on a relationship.
>
> On Sat, Nov 12, 2011 at 10:35 AM, Matthieu Bosquet
> <matthieubosquet@gmail.com> wrote:
> > Hi,
> > Could someone please point me to an example of add method for a
> many-to-many
> > relationship using SQLAlchemy ?
> > @Armin : It would be very nice to have such an example in the
> > FlaskSQLAlchemy documentation
> >
> > In the best of the world, we would even have a Flaskr2 demo app :
> >   - It would have 2 extra tables Tag and Tags
> >   - It would use SQLAlchemy
> >   - It would show a clever use of JQuery to autocomplete the tags in the
> UI
> >
> > I'd be happy to help making that happen.
> >
> > Cheers,
> > Matthieu
> >
>