ok, the "get" on mapper will "get" the object that has been *loaded* from the database already, or if not will try to load it. creating your own objects in memory and setting the "primary key attribute" on them wont do anything...as far as SA is concerned, an unsaved object doesnt really have a "primary key" until you try to save it, which it cant because youve hardwired your primary keys to an invalid state (i.e. two objects with the same value).

what youre doing here is creating two distinct objects and setting their primary key to be the same value, which is, well, it wont work.

if the "c1" column in your datafile has "xyz" twice, then "c1" is not your primary key. you either have to identify a truly unique column in your datafile, *or* you have to break it up so that a single entry correponds to a parent object and a child object, and also insure that you only create one object per primary key, such as:

x = C1('xyz')
y = C1('abc')
z = C2('zz')
z2 = C2('xx')

x.c2s.append(z)
x.c2s.append(z2)
y.c2s.append(z2)

Consider using a dictionary to make unique objects, such as:

        def make_c1(key):
                try:
                        return c1dict[key]
                except KeyError:
                        x = C1(key)
                        c1dict[key] = x
                        return x
                
hope this helps...

On Feb 17, 2006, at 4:36 PM, Mohan K wrote:

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



-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642
_______________________________________________
Sqlalchemy-users mailing list
Sqlalchemy-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users

Reply via email to