Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #104746

Re: Psycopg2 to create a record using a FK

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Aaron Christensen <aaron.christensen@gmail.com>
Newsgroups comp.lang.python
Subject Re: Psycopg2 to create a record using a FK
Date Sat, 12 Mar 2016 22:47:36 -0500
Lines 168
Message-ID <mailman.54.1457840866.12893.python-list@python.org> (permalink)
References <CAOA=+NtOje98Qkx3bG6Yi8QxwBt2LsRyQfsLAvrNWvbcWKDQug@mail.gmail.com> <87vb4sdnd7.fsf@handshake.de> <CAOA=+Nv=9oXq27=9Wtf3GoyaRW3A09L+VYQuD0a9Bi0k+371eQ@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de Y4UQOMDk8akJkvOWDuiX6gKUwAAnZoV5XX12HQYswWig==
Return-Path <aaron.christensen@gmail.com>
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; 'seemed': 0.07; 'table.': 0.07; 'cc:addr:python-list': 0.09; 'activity.': 0.09; 'command.': 0.09; 'cursor': 0.09; 'derived': 0.09; 'integers': 0.09; 'postgres': 0.09; 'subject:create': 0.09; 'subject:using': 0.09; 'tuple': 0.09; 'received:209.85.218': 0.10; 'python': 0.10; 'question.': 0.13; '"insert': 0.16; '%s,': 0.16; '(%s,': 0.16; '2016': 0.16; 'argument.': 0.16; 'bind': 0.16; 'decent': 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; '&gt;': 0.18; 'creates': 0.18; 'email addr:gmail.com&gt;': 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; '&quot;': 0.29; '(maybe': 0.29; 'dictionary': 0.29; 'figured': 0.29; 'gender,': 0.29; "i'm": 0.30; 'url:mailman': 0.30; 'work.': 0.30; 'code': 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; 'thanks!': 0.34; 'handle': 0.34; 'running': 0.34; 'add': 0.34; 'skip:& 20': 0.35; 'received:google.com': 0.35; 'skip:c 30': 0.35; 'next': 0.35; 'done': 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; 'pm,': 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; 'url:mail': 0.40; 'build': 0.40; 'your': 0.60; 'close': 0.61; 'hope': 0.61; '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; 'finally': 0.70; 'out!': 0.84; 'out..': 0.84; 'programs:': 0.84; 'trick.': 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=Xaab149TytpAmEJy70TDCy4o4rK7nipv6ow6CMfqsMs=; b=V73ZBpM8M2/ZcOe5Z13xZrUrcYx9vwmwnp6qhNBrodl5pUHUXh7GaguOmeigHQ6rIr /ZZNd11PAcBWwdTVuPHcvH+o6L85Tq9nN/c8lKzoGXNkM0HEJ8XnKT2fVhTCyh/YlUGK mJy/j0J7aOHQfvz2OVdwF3zk7JWTma+MRkBCKc0PAmiin+xoL6JBS3Uf+6+oXTh7Yozd JAHa4barCNEEgQSoMevYjR7YhQq+Hn6LbUkc2Mg74azxH8O3Y/CTLWfg5lzc6QuzrD3C Rsawf1MjOKbFfZ2udTAa11KuoXOQG5tkf9o0ETERi7NFolPavZtHLyVfXyILLW6Kb9ri OH3w==
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=Xaab149TytpAmEJy70TDCy4o4rK7nipv6ow6CMfqsMs=; b=KmT9fF9n5ya1MUgukGx3qVX65O3BkG2LtaUSQy4DdtW2xN1c/SwE/PXee/9IVLPz8/ 1x2KVrtgbMFjpFwrklJuKLQP5IJikDIkZYxpAeFJRPHo8PFvXL15T6Hjurll+q6NdeZz nXn3VAynvPLtwigeg9uKC3yLZZGVS2N0cuq9fqvkXBt4MIzcaFHGljojBRPx12GnIqOF ZQDQ+siyuKdxprHaufavfJ3dsDwGITtC7f4PhQ2U4l/okitmJrFdgmlddeodsxM/euDH EtGxU7ALb64T0lI/FkNu6zyZAcEd4SAmLFwmUXJRorpf2TleiuQc1LDTBo45wCOx/8E9 WRcQ==
X-Gm-Message-State AD7BkJJEqRfU9Z0+oW9QGDfHKZlsm37pnLvz0GOVS7HJ+ycW/CFKdYX1RYGr23Hc8ULPdpWB3VYUEeBQZzqCbQ==
X-Received by 10.202.202.143 with SMTP id a137mr9896130oig.28.1457840856891; Sat, 12 Mar 2016 19:47:36 -0800 (PST)
In-Reply-To <CAOA=+Nv=9oXq27=9Wtf3GoyaRW3A09L+VYQuD0a9Bi0k+371eQ@mail.gmail.com>
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 <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:104746

Show key headers only | View raw


On Sat, Mar 12, 2016 at 9:57 PM, Aaron Christensen <
aaron.christensen@gmail.com> wrote:

>
>
> On Sat, Mar 12, 2016 at 5:03 AM, dieter <dieter@handshake.de> wrote:
>
>> Aaron Christensen <aaron.christensen@gmail.com> 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)
>


Dieter,

I think I finally figured it out!

In this section:
UserId = cur.fetchone()[0]        #< ----------   HERE is the UserId
print("UserId = %s" % UserId)

I added user_id to the dictionary and passed the dictionary to the
cur.execute():
user_id = cur.fetchone()[0]
user_profile_input['user_id'] = user_id

cur.execute("""
INSERT INTO UserProfile
(...)
VALUES (...)
RETURNING UserProfileId""",
(user_profile_input)

That seemed to have done the trick.  However, I hope that the way my code
is structured is a decent design with the multiple conn.commit()s.  Should
I need to close the cursor after every Table cur.execute() command?

Thanks!
Aaron

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Psycopg2 to create a record using a FK Aaron Christensen <aaron.christensen@gmail.com> - 2016-03-12 22:47 -0500

csiph-web