1

Hope you can help me!

I have a relationship in sqlalchemy as follows:

class Location(db.Model, DateModified, Sluggable):
    __tablename__ = 'location'
    __sluggable__ = {
        'populate_from': 'title',
        'always_update': True
    }    
    id = db.Column(db.Integer, primary_key=True)

    title = db.Column(db.String(80))

    ...

    parent_location_id = db.Column(db.Integer, db.ForeignKey('location.id'), index=True)
    parent_location = db.relationship(lambda: Location, remote_side=id, backref='child_locations')

I'm really uncertain as to how to debug the fact that intermittently flask-admin or sqlalchemy is loosing the parent_location_id when the parent location is saved.

This disappearing act seems to be fairly random.

Anyone with any clue as to how I debug this would help immensely!

Thanks.

pip
  • 453
  • 2
  • 13

1 Answers1

0

Joes, thanks for reminding me to answer this.

It was simply that I had a backref in my models that was hidden on the form. Therefore, the data being submitted was empty clearing the relationships.

Moral of the story: Don't have back refs that are hidden in your admin forms.

pip
  • 453
  • 2
  • 13
  • Dear pip, do you know is there any way to display the field, set it as readonly, and when submitting the form, also render it's value to server side, so that it won't be delete from DB? – Lawrence Liu Jun 18 '15 at 05:11
  • http://stackoverflow.com/questions/14874846/readonly-text-field-in-flask-admin-modelview/23959450#23959450 – pip Jun 18 '15 at 09:36