{"action":"created","issue":{"url":"https://api.github.com/repos/mitsuhiko/flask/issues/1490","labels_url":"https://api.github.com/repos/mitsuhiko/flask/issues/1490/labels{/name}","comments_url":"https://api.github.com/repos/mitsuhiko/flask/issues/1490/comments","events_url":"https://api.github.com/repos/mitsuhiko/flask/issues/1490/events","html_url":"https://github.com/mitsuhiko/flask/issues/1490","id":86373021,"number":1490,"title":"Get “insert_id” for one to one relationship in Flask, SqlAlchemy","user":{"login":"JeanClaude3","id":12805275,"avatar_url":"https://avatars.githubusercontent.com/u/12805275?v=3","gravatar_id":"","url":"https://api.github.com/users/JeanClaude3","html_url":"https://github.com/JeanClaude3","followers_url":"https://api.github.com/users/JeanClaude3/followers","following_url":"https://api.github.com/users/JeanClaude3/following{/other_user}","gists_url":"https://api.github.com/users/JeanClaude3/gists{/gist_id}","starred_url":"https://api.github.com/users/JeanClaude3/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/JeanClaude3/subscriptions","organizations_url":"https://api.github.com/users/JeanClaude3/orgs","repos_url":"https://api.github.com/users/JeanClaude3/repos","events_url":"https://api.github.com/users/JeanClaude3/events{/privacy}","received_events_url":"https://api.github.com/users/JeanClaude3/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":5,"created_at":"2015-06-08T23:39:36Z","updated_at":"2015-06-09T07:30:09Z","closed_at":null,"body":"Hey guys,\r\n\r\nI tried seeking help on SO, but couldn't get any feedback.  Thought I'd reach out to the Flask community.\r\n\r\nI'm trying to insert a new row through sqlalchemy. The parent table (Milestone) has a child table called Funding. Both tables share a relationship through a column called milestone_id. It is a one to one relationship.\r\n\r\nI have looked it up, but I can't figure out how to reference the milestone_id when INSERTING a new row in the Funding table. The parent ID is an autoincrement. I am using Flask and SqlAlchemy.\r\n\r\nmodels:\r\n```python\r\nclass Milestone(db.Model):\r\n    __tablename__ = \"**************\"\r\n\r\n   milestone_id = db.Column(db.Integer, primary_key=True)\r\n   company_id = db.Column(db.Integer, db.ForeignKey('stlines_startups.company_id'))\r\n   milestone_date = db.Column(db.Integer)\r\n   snapshots = db.relationship('Snapshot', uselist=False, primaryjoin='Milestone.milestone_id==Snapshot.milestone_id', backref='milestone')\r\n   fundraising = db.relationship('Funding', uselist=False, primaryjoin='Milestone.milestone_id==Funding.milestone_id', backref='milestone')\r\n\r\n   def __init__(self, milestone_id, company_id, milestone_date, snapshots = [], fundraising = []):\r\n    self.milestone_id = milestone_id\r\n    self.company_id = company_id\r\n    self.milestone_date = milestone_date\r\n    self.snapshots = snapshots\r\n    self.fundraising = fundraising\r\n\r\nclass Funding(db.Model):\r\n    __tablename__ = \"**************************\"\r\n\r\n   funding_id = db.Column(db.Integer, primary_key=True)\r\n   funding_type = db.Column(db.Text)\r\n   funding_message = db.Column(db.Text)\r\n   funding_amount = db.Column(db.Integer)\r\n   milestone_source = db.Column(db.Text)\r\n   company_id = db.Column(db.Integer, db.ForeignKey('stlines_milestones.company_id'))\r\n   milestone_id = db.Column(db.Integer, db.ForeignKey('stlines_milestones.milestone_id'))\r\n   user_id = db.Column(db.Integer)\r\n   funding_timestamp = db.Column(db.Integer)\r\n\r\ndef __init__(self, funding_id, funding_type, funding_message, funding_amount, milestone_source, milestone_id, company_id, user_id, funding_timestamp):\r\n    self.funding_id = funding_id\r\n    self.funding_type = funding_type\r\n    self.funding_message = funding_message\r\n    self.funding_amount = funding_amount\r\n    self.milestone_source = milestone_source\r\n    self.milestone_id = milestone_id\r\n    self.company_id = company_id\r\n    self.user_id = user_id\r\n    self.funding_timestamp = funding_timestamp\r\n```\r\n\r\nAlchemy Query:\r\n```python\r\n@app.route('/_add_funding')\r\ndef add_funding():\r\n    funding_type = request.args.get('funding_stage', '', type=str)\r\n    funding_message = request.args.get('funding_message', '', type=str)\r\n    funding_amount = request.args.get('funding_amount', 0, type=int)\r\n    milestone_source = request.args.get('milestone_source', '', type=str)\r\n    milestone_date = request.args.get('milestone_date', '', type=str)\r\n    company_id = request.args.get('company_id', '', type=int)\r\n\r\n    milestone_date_final = datetime.datetime.strptime(milestone_date, '%B %d, %Y')\r\n\r\n    ''' In this line, I try to reference milestone_id with new_milestone.milestone_id, but nothing shows up in the database '''\r\n    new_funding = Funding('', funding_type=funding_type, funding_message=funding_message, funding_amount=funding_amount, milestone_source=milestone_source, company_id=company_id, milestone_id=new_milestone.milestone_id, user_id='1', funding_timestamp=milestone_date_final)\r\n    new_milestone = Milestone('', company_id=company_id, milestone_date=milestone_date_final, snapshots=None, fundraising=new_funding)\r\n    db.session.add(new_milestone)\r\n    output = new_milestone.milestone_id\r\n    db.session.commit()\r\n\r\n    return jsonify(result=output)\r\n```\r\nHow can I tell SqlAlchemy to use the auto generated milestone_id from the milestone table when insert the funding information in the funding table? Should these be two separate queries?  When I do them as two separate queries I get the error: sqlalchemy.orm.exc.ObjectDeletedError: Instance '<Milestone at 0x39ebeb0>' has been deleted, or its row is otherwise not present.\r\n\r\n\r\nAny help would be greatly appreciated, thanks guys :)"},"comment":{"url":"https://api.github.com/repos/mitsuhiko/flask/issues/comments/110260245","html_url":"https://github.com/mitsuhiko/flask/issues/1490#issuecomment-110260245","issue_url":"https://api.github.com/repos/mitsuhiko/flask/issues/1490","id":110260245,"user":{"login":"pussbb","id":335847,"avatar_url":"https://avatars.githubusercontent.com/u/335847?v=3","gravatar_id":"","url":"https://api.github.com/users/pussbb","html_url":"https://github.com/pussbb","followers_url":"https://api.github.com/users/pussbb/followers","following_url":"https://api.github.com/users/pussbb/following{/other_user}","gists_url":"https://api.github.com/users/pussbb/gists{/gist_id}","starred_url":"https://api.github.com/users/pussbb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pussbb/subscriptions","organizations_url":"https://api.github.com/users/pussbb/orgs","repos_url":"https://api.github.com/users/pussbb/repos","events_url":"https://api.github.com/users/pussbb/events{/privacy}","received_events_url":"https://api.github.com/users/pussbb/received_events","type":"User","site_admin":false},"created_at":"2015-06-09T07:30:09Z","updated_at":"2015-06-09T07:30:09Z","body":"e.g. http://docs.sqlalchemy.org/en/latest/orm/basic_relationships.html\r\n```python\r\n# create parent, append a child via association\r\np = Parent()\r\na = Association(extra_data=\"some data\")\r\na.child = Child()\r\np.children.append(a)\r\n\r\n```"}}