Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Peter Otten <__peter__@web.de> Newsgroups: comp.lang.python Subject: Re: Psycopg2 to create a record using a FK Date: Sat, 12 Mar 2016 11:26:45 +0100 Organization: None Lines: 80 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Trace: news.uni-berlin.de BzFkE+zfbQ+sjrMKEl1dwQH1VhortfqO8zzYNqWUgLww== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'postgresql': 0.07; 'activity.': 0.09; 'command.': 0.09; 'derived': 0.09; 'postgres': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:create': 0.09; 'subject:using': 0.09; 'python': 0.10; 'question.': 0.13; '%s,': 0.16; '(%s,': 0.16; 'dict(': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'wrote:': 0.16; 'later': 0.16; "wouldn't": 0.16; 'creates': 0.18; 'versions': 0.20; 'insert': 0.23; 'second': 0.24; 'tried': 0.24; "i've": 0.25; 'header:User-Agent:1': 0.26; 'header:X-Complaints- To:1': 0.26; "skip:' 10": 0.28; 'values': 0.28; 'record': 0.29; 'gender,': 0.29; 'query': 0.30; "can't": 0.32; 'table': 0.32; 'point': 0.33; 'extract': 0.33; 'structure': 0.34; 'handle': 0.34; 'skip:d 20': 0.34; 'running': 0.34; 'returning': 0.35; 'but': 0.36; 'should': 0.36; 'there': 0.36; 'possible': 0.36; 'to:addr :python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'seem': 0.37; 'starting': 0.37; 'associated': 0.38; 'thank': 0.38; 'build': 0.40; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'hello,': 0.40; 'your': 0.60; 'skip:u 10': 0.61; 'gender': 0.91; 'subject:record': 0.91 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: p57bd8360.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:104685 Aaron Christensen wrote: > Hello, > > I am running the following versions of software: > > Python 3.5 > psycopg2==2.6.1 > Postgres 9.4.5 > > I have 2 tables. Table User has UserId (serial PK), LastName, FirstName, > Gender, DateOfBirth, and DateEnrolled. Table UserProfile has > UserProfileId > (serial, PK), UserId (FK), DateEntered, FaveNumber, and Activity. There > is a one-to-many relationship. > > The following PostgreSQL works and ultimately creates a record in > UserProfile with an associated UserId (FK). > > \set last_name '''Sara''' > \set first_name '''Jackson''' > \set gender '''F''' > \set dob '''1941-1-12''' > \set fave_number '''3''' > \set activity '''volleyball''' > > > WITH ins_user AS ( > INSERT INTO User > (LastName, FirstName, Gender, DateOfBirth, DateEnrolled) > VALUES (:last_name, :first_name, :gender, :dob, now()) > RETURNING UserId) > INSERT INTO UserProfile > (UserId, DateEntered, FaveNumber, Activity) > VALUES ( (SELECT UserId FROM ins_user), now(), :fave_number :activity); > > How can I build a psycopg2 cur.execute query that will accomplish the > above > PostgreSQL? I've read documentation but can't seem to get a handle on how > I should structure this command. I have not tried it, but wouldn't the straight-forward cur.execute(""" WITH ins_user AS ( INSERT INTO User (LastName, FirstName, Gender, DateOfBirth, DateEnrolled) VALUES (:last_name, :first_name, :gender, :dob, now()) RETURNING UserId) INSERT INTO UserProfile (UserId, DateEntered, FaveNumber, Activity) VALUES ( (SELECT UserId FROM ins_user), now(), :fave_number :activity); """, dict( first_name="Sara", last_name="Jackson", gender="F", dob=datetime.date(1941, 1, 12), fave_number=3, activity="volleyball" )) work? > My starting point is: > > cur.execute( \ > """INSERT INTO User \ > (LastName, FirstName, Gender, DateOfBirth, DateEnrolled) \ > VALUES (%s, %s, %s, %s, %s) RETURNING UserId;""", \ > (last_name, first_name, gender, date_of_birth, now(), ??...??) > > > Also, I have a second question. Is it possible to extract that value > derived from "RETURNING UserId" so that it can be used in a later query? > > Thank you for your time! > Aaron