Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Aaron Christensen Newsgroups: comp.lang.python Subject: Re: Psycopg2 to create a record using a FK Date: Sat, 12 Mar 2016 21:57:02 -0500 Lines: 133 Message-ID: References: <87vb4sdnd7.fsf@handshake.de> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de 4dpwYK+b49IWWK0nX/p9uQZlFGfOgkhwbgUFPpVyZJbA== 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; 'initialize': 0.05; 'indices': 0.07; 'postgresql': 0.07; 'table.': 0.07; 'cc:addr :python-list': 0.09; 'activity.': 0.09; 'command.': 0.09; 'derived': 0.09; 'integers': 0.09; 'postgres': 0.09; 'subject:create': 0.09; 'subject:using': 0.09; 'tuple': 0.09; 'python': 0.10; 'question.': 0.13; '"insert': 0.16; '%s,': 0.16; '(%s,': 0.16; '2016': 0.16; 'argument.': 0.16; 'bind': 0.16; 'dict(': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'row': 0.16; 'wrote:': 0.16; 'later': 0.16; 'skip:u 30': 0.18; '>': 0.18; 'creates': 0.18; 'email addr:gmail.com>': 0.18; 'input': 0.18; 'versions': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; '%s"': 0.22; 'skip:% 10': 0.22; 'pass': 0.22; 'trying': 0.22; 'am,': 0.23; 'dependent': 0.23; 'insert': 0.23; 'passing': 0.23; 'sat,': 0.23; 'second': 0.24; 'tried': 0.24; 'header:In-Reply-To:1': 0.24; '----------': 0.25; "i've": 0.25; 'command': 0.26; 'figure': 0.27; 'message- id:@mail.gmail.com': 0.27; 'this.': 0.28; "skip:' 10": 0.28; 'skip:u 20': 0.28; 'values': 0.28; 'skip:( 20': 0.28; 'record': 0.29; '"': 0.29; '(maybe': 0.29; 'dictionary': 0.29; 'gender,': 0.29; "i'm": 0.30; 'url:mailman': 0.30; 'work.': 0.30; 'query': 0.30; "can't": 0.32; 'table': 0.32; 'point': 0.33; 'url:python': 0.33; 'extract': 0.33; 'url:listinfo': 0.34; 'structure': 0.34; 'handle': 0.34; 'running': 0.34; 'add': 0.34; 'skip:& 20': 0.35; 'received:google.com': 0.35; 'next': 0.35; 'returning': 0.35; 'but': 0.36; 'should': 0.36; 'there': 0.36; 'url:org': 0.36; 'created': 0.36; 'received:209.85': 0.36; 'possible': 0.36; 'subject:: ': 0.37; 'skip:& 10': 0.37; 'thanks': 0.37; '12,': 0.37; 'suggestion': 0.37; 'seem': 0.37; 'starting': 0.37; 'associated': 0.38; 'received:209': 0.38; 'skip:p 20': 0.38; 'sure': 0.39; 'format': 0.39; 'received:209.85.214': 0.39; 'url:mail': 0.40; 'build': 0.40; 'your': 0.60; 'skip:u 10': 0.61; 'back': 0.62; 'latest': 0.64; 'mar': 0.65; 'response.': 0.66; 'here': 0.66; 'skip:\xc2 10': 0.67; 'out..': 0.84; 'programs:': 0.84; 'activity,': 0.91; 'gender': 0.91; 'subject:record': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc; bh=+12XIXMoJWMCH2d1DmHCyXFX8UO6qb1TgesDxYC1VtI=; b=mIDWvQZ4aGw97PxCGUcogqUKJDYljvakVrxlfoW5VwXp5gi13PCORZ/Poa3R/rLzTi 88aGcBW/iUwp1fwaAuN8t/RKdRck+P/nrM/VCZeCvDanGGKrxu32B38EA8n0DxwaWlez ms98YKXXlAhAHBQ++8o6hHrmZ0xYh94AQ6JdLrEciti7+tCcsyNq9YiHPv7IztaANdmY OCfM+FMZ513WM6ylOXr/BoMLRjUMav1RsKhKQoEeu6P6SAwbiuEzlWiqJysbROrxKXFo EpshgxrfLO7TS+lO9qMQBHGDggO5LLf9NupU1eP3G/RKCtI2aIhDwTCVHXn1ECiILuuQ iLxg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc; bh=+12XIXMoJWMCH2d1DmHCyXFX8UO6qb1TgesDxYC1VtI=; b=R1myCiVLFJSicaJgbkgq2og8f3RsX/4qpe3I7YsdTQNg9PGnxMB+xjhUaK8WOM5wIU muYamJrh5bwfTslzqSzkFuxTTSRkFBbY0BZ16WuKHy5H4pQ7sw8GOaEkpbshZDfcnz2K GM22SyVmci0FLU+QlmJ5AYVsSag6EmNTzAf4s1+566h8YJMX0yBqtLg7tTVI2GdCByMr XqYkM1WwjAlobuQj1P2HBHKcqzq17YqxeOQoYoy8i3y02JjX2Ut/eOGV1uwUgh0x3Zwh Vu4DCr3eLgOaQTOCzDpZnx7ljo5s+A3IeKbM35OTxOcVeiFqKz6vLT7xrMFF0gNjgqfD Ttaw== X-Gm-Message-State: AD7BkJK2nkfIe6itQ2UhB/5OQwYkm6WYipsKrPME8a9dgPwflRL6/flgQPjFmCpRCZN5PVPIHzbmFpfxDvne6Q== X-Received: by 10.182.107.137 with SMTP id hc9mr9894864obb.74.1457837822107; Sat, 12 Mar 2016 18:57:02 -0800 (PST) In-Reply-To: <87vb4sdnd7.fsf@handshake.de> X-Content-Filtered-By: Mailman/MimeDel 2.1.21 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:104742 On Sat, Mar 12, 2016 at 5:03 AM, dieter wrote: > Aaron Christensen writes: > > 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. > > > > 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(), ??...??) > > You can add "returning UserId" to this SQL command to get back > the id of the created user in your Python program. You can > then use this "UserId" to create the row in your dependent table. > > I use it like this in one of my programs: > > cursor.execute("insert into service(...) " > "values (...) returning id", > (...) > ) > id = cursor.fetchone()[0] > cursor.execute( > "insert into product(..., f_service_id) " > "values (..., %s) returning id", > (..., id) > ) > id = cursor.fetchone()[0] > > > > Likely, there is also a way to bind the "UserId" inside SQL (maybe > via "SET") and use it in a second "INSERT" in the same call > to "cur.execute". Check the Postgres documentation for this. > > > > 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? > > Sure -- see above. > > -- > https://mail.python.org/mailman/listinfo/python-list Hi Dieter, Thanks for the response. I managed to get it working and also combined it with Peter's suggestion of passing a dictionary as an argument. However, I am trying to figure out how I can get the RETURNING ID to be used in the next cur.execute(). Here is what I have been working on but have found many ways for it not to work. My latest response is that the tuple indices must be integers or slices. # Here I initialize the dictionaries. I will use each dictionary as an input into each cur.execute() user_input = dict( last_name = 'Jackson', first_name = 'Sara', gender = 'F', date_of_birth = '1941-1-12' ) user_profile_input = dict( fave_number = 3, activity = 'volleyball' ) # Create record in User // cur.execute(query, user_input) cur.execute(""" INSERT INTO User (LastName, FirstName, Gender, DateOfBirth) VALUES (%(last_name)s, %(first_name)s, %(gender)s, %(date_of_birth)) RETURNING UserId""", user_input) conn.commit() UserId = cur.fetchone()[0] #< ---------- HERE is the UserId print("UserId = %s" % UserId) # Create record in UserProfile // cur.execute(query, user_profile_input) cur.execute(""" INSERT INTO UserProfile (FaveNumber, Activity, UserId) VALUES (%(fave_number)s, %(activity)s, %s) <------------ I tried following your format RETURNING UserProfileId""", (user_profile_input, UserId) # <---- This is what I'm trying to figure out.. How to pass the UserId. ) conn.commit() UserProfileId = cur.fetchone()[0] print("UserProfileId = %s" % UserProfileId)