{"comment":{"url":"https://api.github.com/repos/BUADCP2013/CS473-EX_CMS/comments/10327037","html_url":"https://github.com/BUADCP2013/CS473-EX_CMS/commit/67b37517f15fb5e9a2fdd8fd43226538485b1c57#commitcomment-10327037","id":10327037,"user":{"login":"DirtNap","id":410264,"avatar_url":"https://avatars.githubusercontent.com/u/410264?v=3","gravatar_id":"","url":"https://api.github.com/users/DirtNap","html_url":"https://github.com/DirtNap","followers_url":"https://api.github.com/users/DirtNap/followers","following_url":"https://api.github.com/users/DirtNap/following{/other_user}","gists_url":"https://api.github.com/users/DirtNap/gists{/gist_id}","starred_url":"https://api.github.com/users/DirtNap/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/DirtNap/subscriptions","organizations_url":"https://api.github.com/users/DirtNap/orgs","repos_url":"https://api.github.com/users/DirtNap/repos","events_url":"https://api.github.com/users/DirtNap/events{/privacy}","received_events_url":"https://api.github.com/users/DirtNap/received_events","type":"User","site_admin":false},"position":6,"line":null,"path":"letterbucket/content/posts.py","commit_id":"67b37517f15fb5e9a2fdd8fd43226538485b1c57","created_at":"2015-03-21T01:35:49Z","updated_at":"2015-03-21T01:35:49Z","body":"The way it works is that the child gets a foreign key pointing at the parent, the parent gets a relationship to the child, and that relationship contains a `backref` argument which creates a relationship property in the child automatically. The foreign key is how both of the relationships know what to do.  Here's an example:\n\n```python \n    class Parent(db.Model):\n        id = db.Column(db.Integer, primary_key=True)\n        children = db.relationship('Child', backref=\"parent\")  # class Parent has a children property which is an array of all its children.  class Child has a parent property pointing to its parent object\n\n    class Child(db.Model):\n        id = db.Column(db.Integer, primary_key=True)\n        parent_id = db.Column(db.Integer, db.ForeignKey('parent.id'))  # This points to the parent table's id column\n\n    p = Parent()\n    p.id = 1\n    c = Child()\n    c.id = 777\n    print c.parent.children[0].parent.children[0].parent.id. # Prints \"1\" -- see how the 2 way relationship is here?"}}