Greetings,
This is my first dab at SQLAlchemy, please bear with me if I am doing something wrong. The
library looks awesome,BTW. Ok, I am trying to load a table via SQLAlchemy. The table basi-
cally has two columns (postgres):
  c1: varchar(64) primary key
  c2: varchar(3)
 
My data file is a list of entries like:
c1       c2
---       ---
xyz      zz
abc      xx
xyz       xx
...
...

Notice, the data file does have some duplicate entries for primary key. Now all I do is
a_table = Table('a',....)
Class A(object):
  pass
a_mapper = mapper(A,a_table)

And then I have a for loop where I read each line in the file create the object
a_o = A()

And after the loop exits, I want do a commit:
objectstore.commit()

Of course, this bombs out with "duplicate key" error.
After some digging, it appears that when I create new objects the UoW does not check the
primary key constraint for 'c1' and happily creates the objects? Is this normal behavior?

So to alleviate that before creating the new object (a_o) I added logic to check if it already
exists in the objectstore by issuing:

a_mapper.get(pk_value)

Strangely, this is always returning None. From the docs, it appeared that this should return
the in-memory object and then try to fetch from db. But the behavior I am seeing is that
it *only* tries to fetch from the Db.

I can get it to load by doing a commit() right after I create the object, but I want to avoid the
round trips to the db.

Anyways, I am not sure, but shouldn't the "get" metthod of Mapper class  do a
tuple(ident) instead of just passing ident to the various method calls in it?

Thanks
Mohan

Reply via email to