Вопрос

I'm attempting to create a database for events and event RSVPs...

For each event there should be possible food items for an rsvp to choose from

When a user RSVP's, the rsvp model created for them should contain the food item they chose so far..

this is what I have so far:

event_meals = db.Table('event_meals',
                       db.Column('meal_id', db.Integer, db.ForeignKey('meal.id')),
                       db.Column('event_id', db.Integer, db.ForeignKey('event.id')))

rsvp_meals = db.Table('rsvp_meals',
                      db.Column('meal_id', db.Integer, db.ForeignKey('meal.id')),
                      db.Column('rsvp_id', db.Integer, db.ForeignKey('rsvp.id')))

class Event(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(256), unique=True)
    date = db.Column(db.String(128))
    photo = db.Column(db.String(256))
    info = db.Column(db.String(1024))
    meals = db.relationship('Meal', secondary='event_meals',
                            backref=db.backref('meals', lazy='dynamic'))

    def __repr__(self):
        return self.title


class Rsvp(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(128))
    price = db.Column(db.String(64))
    meals = db.relationship('Meal', secondary='rsvp_meals',
                            backref=db.backref('meals', lazy='dynamic'))

    def __repr__(self):
        return self.name


class Meal(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(128))
    price = db.Column(db.Integer)
    qty = db.Column(db.Integer)

    def __repr__(self):
        return self.title

Not surprisingly this gives an error:

sqlalchemy.exc.ArgumentError: Error creating backref 'meals' on relationship 'Rsvp.meals': property of that name exists on mapper 'Mapper|Meal|meal'

Can anyone help me make this work?! Help is much appreciated!

Это было полезно?

Решение

backref is the name that should be used to expose the many-to-many relationship on the related model. In your case, you are saying that both Event and Rsvp models should be exposed as the attribute meals on the Meal model. In other words, when I have a Meal and I want to see who has RSVP'd, I should access the meals attribute on the Meal instance. However, you are also declaring that if I want to see what Events the meal is associated with, I should access the meals attribute of the meal ... which is why SQLAlchemy throws an error.

What you most likely meant is to expose rsvps and events on Meals:

class Event(db.Model):
    # ... snip ...
    meals = db.relationship('Meal', secondary='event_meals',
                            backref=db.backref('events', lazy='dynamic'))

# ... snip ...    

class Rsvp(db.Model):
    # ... snip ...
    meals = db.relationship('Meal', secondary='rsvp_meals',
                            backref=db.backref('rsvps', lazy='dynamic'))

Then, for any Meal instance you can query its Events and RSVPs:

for rsvp in Meal.query.get(meal_id).rsvps:
    print rsvp.name

The key thing to note is that SQLAlchemy is a very DRY (Don't Repeat Yourself) framework. If you find yourself having to keep names in sync between different parts of the declaration of the same field, you are probably missing an easier way to do things.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top