Re: [sqlalchemy] Confusion over session.dirty, query, and flush

2018-09-26 Thread Jonathan Vanasco


On Wednesday, September 26, 2018 at 10:08:43 PM UTC-4, jens.t...@gmail.com 
wrote:
>
>
> Suppose I get the “new”, “dirty”, and “deleted” sets as per discussion 
> below, and I’m especially interested in the “dirty” set: is there a way to 
> find out which properties of an object were modified, or only that the 
> object was modified?
>

You want the `inspect`  API

https://docs.sqlalchemy.org/en/latest/core/inspection.html

use `inspect` to get at the InstanceState for the object 
(https://docs.sqlalchemy.org/en/latest/orm/internals.html#sqlalchemy.orm.state.InstanceState)

then use `attrs` on the InstanceState to view the `attrs` which has an 
`AttributeState` with a `history` 
(https://docs.sqlalchemy.org/en/latest/orm/internals.html#sqlalchemy.orm.state.AttributeState)

if you search for 'inspect' in this forum, Michael has provided many 
examples on this topic.

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] Confusion over session.dirty, query, and flush

2018-09-26 Thread jens . troeger
I’d like to pick up this topic once more briefly.

Suppose I get the “new”, “dirty”, and “deleted” sets as per discussion 
below, and I’m especially interested in the “dirty” set: is there a way to 
find out which properties of an object were modified, or only that the 
object was modified?

Thanks!
Jens



On Wednesday, November 22, 2017 at 9:46:54 AM UTC+10, jens.t...@gmail.com 
wrote:
>
> Thank you, the event worked like a charm :-) Though I think that I don't 
> need the commit events, because the application terminates anyway.
>
> I modified your approach to gather which objects were flushed so that in 
> the end I can give the user more precise information:
>
> dbsession.info["new"] = set()
>
> dbsession.info["dirty"] = set()  
>
> dbsession.info["deleted"] = set()
>
>   
>   
> def update_session_info(session):  
>  
> new = session.info["new"]
>
> new |= set(session.new)
>  
> dirty = session.info["dirty"]
>
> dirty |= set(session.dirty)
>  
> deleted = session.info["deleted"]
>
> deleted |= set(session.deleted)
>  
> return new, dirty, deleted
>   
>   
>   
> @event.listens_for(dbsession, "before_flush")  
>  
> def on_before_flush(session, _, _):
>  
> update_session_info(session)
>
> ...
> code.interact(local=locals())  
>  
> ...
>
> new, dirty, deleted = update_session_info(dbsession)  
>   
> if new or dirty or deleted:
>  
> if new:
>  
> print("The following objects were created: ", new)
>
> if dirty:  
>  
> print("The following objects were modified: ", dirty)  
> 
> if deleted:
>  
> print("The following objects were deleted: ", deleted)
>
> yesno = input("Would you like to commit this transaction? 
> [y/N] ") 
> if yesno == "y":  
>   
> print("Committing transaction...")
>  
> else:  
>  
> print("Rolling back transaction...")  
>  
> raise _SessionRollbackException()  
>  
>
> # ...this is where the context closes and the transaction commits 
> and the dbsession ends.
>
> Cheers,
> Jens
>
>
>
> On Saturday, November 18, 2017 at 12:03:05 AM UTC+10, Simon King wrote:
>>
>> OK, I think tracking session events seems reasonable. You could do 
>> something like this (completely untested): 
>>
>> from sqalchemy.event import event 
>>
>> @event.listens_for(YourSessionOrSessionMaker, 'before_flush') 
>> def on_before_flush(session, flush_context, instances): 
>> session.info['flushed'] = True 
>>
>>
>> # You'd probably also want to reset the 'flushed' flag 
>> # after a commit or rollback 
>> @event.listens_for(YourSessionOrSessionMaker, 'after_commit') 
>> @event.listens_for(YourSessionOrSessionMaker, 'after_rollback') 
>> def on_session_reset(session): 
>> session.info['flushed'] = False 
>>
>>
>> # when user exits interactive session: 
>> modified = ( 
>> session.info.get('flushed', False) 
>> or session.deleted 
>> or session.new 
>> or session.dirty 
>> ) 
>> if modified: 
>> raw_input('do you want 

Re: [sqlalchemy] Hybrid property with subquery

2018-09-26 Thread YKdvd
I think that got me to where I have a working hybrid.  It looks something 
like this (I broke it up into multiple statements for debugging):

@isLast.expression
def isLast(cls):
dmsq = aliased(dm, name="dmsq")
q = cls.id == select([dmsq.id]).where(dmsq.department_id == 
cls.department_id).order_by(dmsq.seqOrder.desc()).limit(1).as_scalar()
print q
return q

The subquery is really just sort of a lookup, and leaving out the 
correlate() doesn't seem to chnge the query.  But since "cls" refers to the 
same object model ("dm"), I did have to use an alias ("dmsq") of it for the 
subquery.  I'm using MySQL, so the LIMIT() works.  This seems to be doing 
the right thing with my small test case, and the printed SQL is right, so 
if it works with the full set of data and queries I'll declare victory.  
Thanks again for the help.


On Tuesday, September 25, 2018 at 9:09:24 PM UTC-3, Mike Bayer wrote:
>
> On Mon, Sep 24, 2018 at 12:22 PM YKdvd > 
> wrote: 
> > 
> > I have an ORM setup with a "departments_milestones" table ("dm", 
> DepartmentMilestone objects), with "department_id" and "seqOrder" Fields. 
>  Each department has a few records in this table, ordered within the 
> department by "seqOrder" (so seqOrder is not unique). 
> > 
> > Another object "ShotsStatus" contains a collection of these 
> DepartmentMilestone objects, one for each department.  I'm interested in 
> getting the ShotsStatus records that are at the "last" milestone in its 
> department - the one with the highest "seqOrder" value for the department. 
>  In SQL I can do something like this, with a subquery to select the 
> departmental milestone records, reverse sort them so the 'last' one is 
> first, and LIMIT 1 to get it, which is compared to the outer record. 
> > 
> > select * FROM shots_status ss 
> > left outer join otherschema.departments_milestones dm on 
> (dm.department_id=ss.dept_id AND dm.milestone_id = ss.status_id) 
> > where ss.dept_id=723 
> > and dm.id = (SELECT dmsq.id FROM plumber.departments_milestones dmsq 
> WHERE dmsq.department_id=dm.department_id ORDER BY dmsq.seqOrder DESC LIMIT 
> 1) 
> > 
> > I'd like to create a hybrid property "isLast" on the DepartmentMilestone 
> object that  returns True if it represents the 'last' milestone.  I'm 
> having trouble figuring out what sort of sqlalchemy select coding I'd need 
> to recreate the subquery from the SQL code to do this. 
>
>
> it's a correlated select so it's along the lines of (note this is not 
> the exact SELECT you have): 
>
> @myproperty.expression 
> def myproperty(cls): 
> return select([Dmsq.id]).where(Dmsq.id == 
> cls.dept_id).correlate(cls).order_by(Dmsg.seq).limit(1).as_scalar() 
>
> the LIMIT 1 thing won't work on every backend but should make it on at 
> least MySQL and Postgresql. 
>
>
>
> > 
> > -- 
> > SQLAlchemy - 
> > The Python SQL Toolkit and Object Relational Mapper 
> > 
> > http://www.sqlalchemy.org/ 
> > 
> > To post example code, please provide an MCVE: Minimal, Complete, and 
> Verifiable Example. See http://stackoverflow.com/help/mcve for a full 
> description. 
> > --- 
> > You received this message because you are subscribed to the Google 
> Groups "sqlalchemy" group. 
> > To unsubscribe from this group and stop receiving emails from it, send 
> an email to sqlalchemy+...@googlegroups.com . 
> > To post to this group, send email to sqlal...@googlegroups.com 
> . 
> > Visit this group at https://groups.google.com/group/sqlalchemy. 
> > For more options, visit https://groups.google.com/d/optout. 
>

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] Primary key set on object after rollback

2018-09-26 Thread Mike Bayer
On Wed, Sep 26, 2018 at 10:05 AM Doug Miller  wrote:
>
> Thanks. This occurs on create/insert so I would probably remove all primary 
> keys. Is there a reason why a handler would work better here? I’d prefer to 
> keep my logic central and avoid callbacks if possible.

the event handlers are just a way to change the behavior of the ORM in
ways you'd like.that handler does the behavior you requested.

>
> --
> SQLAlchemy -
> The Python SQL Toolkit and Object Relational Mapper
>
> http://www.sqlalchemy.org/
>
> To post example code, please provide an MCVE: Minimal, Complete, and 
> Verifiable Example.  See  http://stackoverflow.com/help/mcve for a full 
> description.
> ---
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to sqlalchemy+unsubscr...@googlegroups.com.
> To post to this group, send email to sqlalchemy@googlegroups.com.
> Visit this group at https://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] Primary key set on object after rollback

2018-09-26 Thread Doug Miller
Thanks. This occurs on create/insert so I would probably remove all primary 
keys. Is there a reason why a handler would work better here? I’d prefer to 
keep my logic central and avoid callbacks if possible. 

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] Primary key set on object after rollback

2018-09-26 Thread Mike Bayer
On Wed, Sep 26, 2018 at 9:26 AM Mike Bayer  wrote:
>
> On Tue, Sep 25, 2018 at 9:09 PM Doug Miller  wrote:
> >
> > I think my issue is that before the commit/rollback
> > Alice.id is None but after Alice.id is set to some integer. I understand 
> > the object is transient but I wish that the primary key field was not 
> > modified in this way because if I want to try to reinsert Alice in a new 
> > transaction I have to write logic to introspect into the schema and unset 
> > the primary key. This behavior also breaks my mental model of “the object 
> > is the way it was before the commit failure and rollback”.
>
> ideally you'd be making a *new* Alice object altogether.   retry logic
> for failed transactions is usually coarse grained.   the unit of work
> process does not keep track of which attributes on each object were DB
> generated and which weren't and this would be extremely complicated /
> performance-impactful to implement.

note that a transient object can't be "expired" in any case as it is
assumed the object is part of a session context, and a "refresh from
the DB" trigger is added to it which fails if you aren't actually
attached to a Session.

So in this case you want to just set the attribute to None. Here's an
event handler that does that directly:

@event.listens_for(Session, "pending_to_transient")
def intercept_pending_to_transient(session, object_):
pk_attrs = [
attr.key for attr in
inspect(object_).mapper.column_attrs if attr.expression.primary_key
]
for attr in pk_attrs:
delattr(object_, attr)


your test case then prints:

User(name='alice', id=None) User(name='bob', id=None)

now as far as how to distinguish which PK attributes you actually want
to set to None and such, you'd need to add additional logic to detect
the conditions you are looking for.   The event can be set on just an
individual Session object as needed, if that helps.



>
>
>
> >
> > --
> > SQLAlchemy -
> > The Python SQL Toolkit and Object Relational Mapper
> >
> > http://www.sqlalchemy.org/
> >
> > To post example code, please provide an MCVE: Minimal, Complete, and 
> > Verifiable Example.  See  http://stackoverflow.com/help/mcve for a full 
> > description.
> > ---
> > You received this message because you are subscribed to the Google Groups 
> > "sqlalchemy" group.
> > To unsubscribe from this group and stop receiving emails from it, send an 
> > email to sqlalchemy+unsubscr...@googlegroups.com.
> > To post to this group, send email to sqlalchemy@googlegroups.com.
> > Visit this group at https://groups.google.com/group/sqlalchemy.
> > For more options, visit https://groups.google.com/d/optout.

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: File logger fails when used with a Gunicorn/Pyramid .ini file.

2018-09-26 Thread Mike Bayer
On Wed, Sep 26, 2018 at 4:43 AM  wrote:
>
> I haven’t used env.py before, you’re talking about this: 
> https://pypi.org/project/env.py/ , correct?

env.py is an integral part of your Alembic project space and you are
using it.  Please see the tutorial at
https://alembic.zzzcomputing.com/en/latest/tutorial.html#the-migration-environment
. This should likely also be a feature so
https://bitbucket.org/zzzeek/alembic/issues/509/add-support-for-the-here-s-token-when
has been added.

>
> On Wednesday, September 26, 2018 at 10:06:31 AM UTC+10, Mike Bayer wrote:
>>
>> oh, you know that's in the logging.  logging.fileConfig() is used for
>> that and it's actually in your env.py.  Just add "defaults" to that
>> line in your env.py.
>>
>> https://docs.python.org/2/library/logging.config.html#logging.config.fileConfig
>>
>>
> --
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy-alembic" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to sqlalchemy-alembic+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy-alembic" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy-alembic+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] Primary key set on object after rollback

2018-09-26 Thread Mike Bayer
On Tue, Sep 25, 2018 at 9:09 PM Doug Miller  wrote:
>
> I think my issue is that before the commit/rollback
> Alice.id is None but after Alice.id is set to some integer. I understand the 
> object is transient but I wish that the primary key field was not modified in 
> this way because if I want to try to reinsert Alice in a new transaction I 
> have to write logic to introspect into the schema and unset the primary key. 
> This behavior also breaks my mental model of “the object is the way it was 
> before the commit failure and rollback”.

ideally you'd be making a *new* Alice object altogether.   retry logic
for failed transactions is usually coarse grained.   the unit of work
process does not keep track of which attributes on each object were DB
generated and which weren't and this would be extremely complicated /
performance-impactful to implement.



>
> --
> SQLAlchemy -
> The Python SQL Toolkit and Object Relational Mapper
>
> http://www.sqlalchemy.org/
>
> To post example code, please provide an MCVE: Minimal, Complete, and 
> Verifiable Example.  See  http://stackoverflow.com/help/mcve for a full 
> description.
> ---
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to sqlalchemy+unsubscr...@googlegroups.com.
> To post to this group, send email to sqlalchemy@googlegroups.com.
> Visit this group at https://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] Re: How to update a SQLAlchemy database object correctly (test data set and code included).

2018-09-26 Thread Simon King
No problem. By the way, I noticed a bug in my code, on this line:

origin_species =
session.query(Species).filter_by(name=origin_latin_name).one()

You probably want ".first()" rather than ".one()". "first()" will
either return a matching Species object or None, whereas "one()"
checks that the database contains exactly 1 matching row and otherwise
raises an exception.

Simon

On Wed, Sep 26, 2018 at 8:38 AM Ioannes  wrote:
>
> Thank you so much for this detailed response. I really appreciate it, the 
> explanation and example really furthered my understanding and where I was 
> going wrong.
>
> On Sunday, September 23, 2018 at 9:05:36 PM UTC+1, Ioannes wrote:
>>
>> Hi all,
>>
>> I had originally asked a question here: 
>> https://stackoverflow.com/questions/52391072/creating-and-appending-to-a-list-in-sqlalchemy-database-table/52391339#52391339.
>>  However as I am still really stuck, I thought asking to a group more 
>> specifically focussed on SQLAlchemy would be better.
>>
>> I have a table in the database (see below for test file going into 
>> table).The table has two fields, name (the latin name e.g. homo sapiens) and 
>> other names (the common names e.g. human, man). I want to update a field 
>> (other names) in the table, so instead of having:
>>
>> Rana rugosa human
>> Rana rugosa man
>> Rana rugosa frog
>> Rana rugosa cow
>>
>> In the database table, It will return this:
>>
>> Rana rugosa human,man,frog,cow
>>
>> The test_data file looks like this:
>>
>> origin_organismcommon_name tested_organism
>> Rana rugosahuman-
>> Rana rugosaman  -
>> Rana rugosafrog homo sapiens
>> Rana rugosacow  Rana rugosa
>> Rana rugosafrog Rana rugosa
>> Rana rugosafrog -
>> Rana rugosafrog -
>> Rana rugosafroghomo sapiens
>> -  -   -
>> -  -   homo sapiens
>> -  -   -
>> -  -   -
>> -  -   -
>> -  -   -
>> streptococcus pneumoniae-  -
>>
>> The code:
>>
>> import sys
>> from sqlalchemy.orm  import *
>> from sqlalchemy  import *
>> from dbn.sqlalchemy_module  import lib
>> import pd
>>
>> engine = lib.get_engine(user="user", psw="pwd", db="db", 
>> db_host="111.111.111.11")
>> Base = lib.get_automapped_base(engine)
>> session = Session(engine)
>> tbs = lib.get_mapped_classes(Base)
>> session.rollback()
>> df = pd.read_excel('test_data.xlsx', sheet_name = 'test2')
>>
>>
>>
>>
>> for index, row in df.iterrows():
>> origin_latin_name = row['origin_organism'].strip().lower()
>> other_names_name = row['common_name'].strip().lower()
>> tested_species = row['tested_organism'].strip().lower()
>>
>>
>> if origin_latin_name not in [None, "None", "", "-"]:
>> instance = [x[0] for x in 
>> Session.query(session,tbs['species'].name).filter_by(name=origin_latin_name).all()]
>> if origin_latin_name not in instance:
>> origin_species = lib.get_or_create(
>> session,
>> tbs["species"],
>> name = origin_latin_name,
>> other_names = other_names_name
>> )
>>
>> elif origin_latin_name in instance:
>> other_names_query = 
>> Session.query(session,tbs['species'].other_names).filter_by(name=origin_latin_name)
>> other_names_query_list = [x for x in other_names_query]
>> original_list2 = list(set([y for y in x[0].split(',') for x in 
>> other_names_query_list]))
>> if other_names_name not in original_list2:
>> original_list2.append(other_names_name)
>> new_list = ','.join(original_list2)
>> new_names = {'other_names':','.join(original_list2)}
>>
>> origin_species = lib.get_or_create(
>> session,
>> tbs["species"],
>> name = origin_latin_name,
>> other_names = new_list
>> )
>>
>> The part from the elif statement doesn't work. I've ran into two problems:
>>
>> (1) The most recent error I got: NameError: name 'new_list' is not defined
>>
>> (2) another error I got is that I have another table further on
>>
>> map1 = lib.get_or_create(
>> session,
>> tbs["map1"],
>> age_id_id = age,
>>>
>>> name_id_id = origin_species.id
>>>
>>
>> )
>>
>> ...and it said that origin_species object cannot be found, but I think this 
>> is linked to the elif statement, that somehow the origin_species object is 
>> not being updated properly.
>>
>> If anyone could help I would appreciate it.
>
> --
> SQLAlchemy -
> The Python SQL Toolkit and Object Relational Mapper
>
> 

Re: File logger fails when used with a Gunicorn/Pyramid .ini file.

2018-09-26 Thread jens . troeger
I haven’t used env.py before, you’re talking about 
this: https://pypi.org/project/env.py/ , correct?

On Wednesday, September 26, 2018 at 10:06:31 AM UTC+10, Mike Bayer wrote:
>
> oh, you know that's in the logging.  logging.fileConfig() is used for 
> that and it's actually in your env.py.  Just add "defaults" to that 
> line in your env.py. 
>
>
> https://docs.python.org/2/library/logging.config.html#logging.config.fileConfig
>  
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy-alembic" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy-alembic+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[sqlalchemy] Re: How to update a SQLAlchemy database object correctly (test data set and code included).

2018-09-26 Thread Ioannes
Thank you so much for this detailed response. I really appreciate it, the 
explanation and example really furthered my understanding and where I was 
going wrong. 

On Sunday, September 23, 2018 at 9:05:36 PM UTC+1, Ioannes wrote:
>
> Hi all,
>
> I had originally asked a question here: 
> https://stackoverflow.com/questions/52391072/creating-and-appending-to-a-list-in-sqlalchemy-database-table/52391339#52391339.
>  
> However as I am still really stuck, I thought asking to a group more 
> specifically focussed on SQLAlchemy would be better.
>
> I have a table in the database (see below for test file going into 
> table).The table has two fields, name (the latin name e.g. homo sapiens) 
> and other names (the common names e.g. human, man). I want to update a 
> field (other names) in the table, so instead of having:
>
> Rana rugosa human   Rana rugosa man Rana rugosa frogRana rugosa cow
>
> In the database table, It will return this: 
>
> Rana rugosa human,man,frog,cow
>
> The test_data file looks like this:
>
> origin_organismcommon_name tested_organismRana rugosa 
>human-Rana rugosaman  -Rana 
> rugosafrog homo sapiensRana rugosacow 
>  Rana rugosaRana rugosafrog Rana 
> rugosaRana rugosafrog -Rana rugosa
> frog -Rana rugosafroghomo 
> sapiens-  -   --  
> -   homo sapiens-  -  
>  --  -
>--  -   -- 
>  -   -
> streptococcus pneumoniae-  -
>
> The code:
>
> import sys from sqlalchemy.orm  import * from sqlalchemy  import * from 
> dbn.sqlalchemy_module  import lib import pd
>
> engine = lib.get_engine(user="user", psw="pwd", db="db", 
> db_host="111.111.111.11")Base = lib.get_automapped_base(engine)
> session = Session(engine)
> tbs = lib.get_mapped_classes(Base)
> session.rollback()
> df = pd.read_excel('test_data.xlsx', sheet_name = 'test2')
>
>
>
> for index, row in df.iterrows():  
> origin_latin_name = row['origin_organism'].strip().lower()
> other_names_name = row['common_name'].strip().lower()
> tested_species = row['tested_organism'].strip().lower()
>
> if origin_latin_name not in [None, "None", "", "-"]:
> instance = [x[0] for x in 
> Session.query(session,tbs['species'].name).filter_by(name=origin_latin_name).all()]
> if origin_latin_name not in instance:
> origin_species = lib.get_or_create(
> session,
> tbs["species"],
> name = origin_latin_name,
> other_names = other_names_name
> )
>
> elif origin_latin_name in instance:
> other_names_query = 
> Session.query(session,tbs['species'].other_names).filter_by(name=origin_latin_name)
> other_names_query_list = [x for x in other_names_query]
> original_list2 = list(set([y for y in x[0].split(',') for x in 
> other_names_query_list]))
> if other_names_name not in original_list2:
> original_list2.append(other_names_name)
> new_list = ','.join(original_list2)
> new_names = {'other_names':','.join(original_list2)}
>
> origin_species = lib.get_or_create(
> session,
> tbs["species"],
> name = origin_latin_name,
> other_names = new_list
> )
>
> The part from the elif statement doesn't work. I've ran into two problems:
>
> (1) The most recent error I got: NameError: name 'new_list' is not defined
>
> (2) another error I got is that I have another table further on
>
> map1 = lib.get_or_create(
> session,
> tbs["map1"],
> age_id_id = age,
>>
>> name_id_id = origin_species.id
>> 
>
> )
>
> ...and it said that origin_species object cannot be found, but I think 
> this is linked to the elif statement, that somehow the origin_species 
> object is not being updated properly.
>
> If anyone could help I would appreciate it. 
>

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.