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:52:56 -0500 Lines: 94 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de 1MxRRhr0hHq2UxMyWhVy/AfDmCFrQ9CwluRXhTGusH+A== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'postgresql': 0.07; 'cc:addr:python-list': 0.09; 'activity.': 0.09; 'command.': 0.09; 'derived': 0.09; 'postgres': 0.09; 'subject:create': 0.09; 'subject:using': 0.09; 'python': 0.10; 'question.': 0.13; '%s,': 0.16; '(%s,': 0.16; '2016': 0.16; 'argument.': 0.16; 'dict(': 0.16; 'now.\xc2\xa0': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'to:addr:web.de': 0.16; 'wrote:': 0.16; 'later': 0.16; "wouldn't": 0.16; 'skip:l 30': 0.18; '>': 0.18; 'creates': 0.18; 'versions': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'pass': 0.22; 'am,': 0.23; 'insert': 0.23; 'sat,': 0.23; 'second': 0.24; 'tried': 0.24; 'header:In- Reply-To:1': 0.24; "i've": 0.25; 'figure': 0.27; 'message- id:@mail.gmail.com': 0.27; 'turns': 0.27; "skip:' 10": 0.28; 'skip:u 20': 0.28; 'values': 0.28; 'record': 0.29; 'dictionary': 0.29; 'gender,': 0.29; 'url:mailman': 0.30; 'query': 0.30; "can't": 0.32; 'table': 0.32; 'returned': 0.32; 'point': 0.33; 'url:python': 0.33; 'extract': 0.33; 'url:listinfo': 0.34; 'structure': 0.34; 'handle': 0.34; 'skip:d 20': 0.34; 'running': 0.34; 'skip:& 20': 0.35; 'received:google.com': 0.35; 'skip:c 30': 0.35; 'returning': 0.35; 'but': 0.36; 'should': 0.36; 'there': 0.36; 'url:org': 0.36; 'received:209.85': 0.36; 'possible': 0.36; 'subject:: ': 0.37; 'skip:& 10': 0.37; '12,': 0.37; 'seem': 0.37; 'starting': 0.37; 'associated': 0.38; 'received:209': 0.38; 'thank': 0.38; 'received:209.85.214': 0.39; 'url:mail': 0.40; 'build': 0.40; 'hello,': 0.40; 'some': 0.40; 'your': 0.60; 'skip:u 10': 0.61; 'you.': 0.64; 'mar': 0.65; '\xc2\xa0\xc2\xa0': 0.66; 'otten': 0.84; 'peter,': 0.84; '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=Lrnf8XIiA84ZEcJzs0B1E1CtnJm5ImcewXbN5E85riA=; b=Q2TfN5AYyKj8Cg3A2Qjce33pzeGu6NJJ9Yd07fURykAuuAeIv/ROhacxiC3d1AqmqS auzXzgozIvudkL6kdLxPKTApWrXcIaSWWdFWEyg/vQIgIi9g7I1K7hvzXiDlcMKSmmrf pbpoN82eyo5XcrzzvbUKgzvMiYXV3L5a7KUf1XrbeeS7nVvezIjiGO2lQhFwBJHS4yF5 /+mbZap39CwzMSlwQ6TGv7O4DoQWWoWZNh9MsWSjoHbJBPQnp4soj/ao6XoTO2D7rAem pL3cqwq7mDv4ONE4LK1VEJwu7o4Grw8lHsgZte74+MreOiCruxtECECqAQ/M9kth71jW Ja6Q== 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=Lrnf8XIiA84ZEcJzs0B1E1CtnJm5ImcewXbN5E85riA=; b=ZyGP3Eoc9eFwu8znXnZNZh0wxG+6qp9LCpbl79mHrjt0LNUnSsfhLi3aOjulpWa8An mxCpwKQK79jn19/j50Dlf124ENFZbJPpAB1/c74cRaQZqUDc3aLKLuPa8SacXbZR5/kc zJfa4RvUlbmsdF+TMfQCIOS0Gkj9txMQcDmK+qbTyEMNmAyABmLT5kZccHIRpGKTAoll CEFGszwnItIoaRkjiWzHtl139yhsH5XjGV5RhvVf6wdM/xyjZe7+vK8s41NpiLxmTCyI diBnCdBC/soGgz47Mz8KI88tb3t90gK/S2CXC/R+iAWtcMVnYZEBmn+TBc9ZMjfB5tNh Ii4w== X-Gm-Message-State: AD7BkJJNxRmsma35PMOVsZ0ZHO0Zh5/wUjR5WQVAqnrzUtNVT2HoAYf/GeY3czMf32+QImv94uwj9km566qF7w== X-Received: by 10.60.38.37 with SMTP id d5mr10168718oek.50.1457837576935; Sat, 12 Mar 2016 18:52:56 -0800 (PST) In-Reply-To: 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:104741 On Sat, Mar 12, 2016 at 5:26 AM, Peter Otten <__peter__@web.de> wrote: > 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? > Hi Peter, Yes, thank you. It turns out that I can pass a dictionary as an argument. I have combined your response with Dieter's and am running into some issues now. I need to figure out how to pass a dictionary AND the returned Id. > > > 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 > > > -- > https://mail.python.org/mailman/listinfo/python-list >