[sqlalchemy] Session and postgres_returning

2008-11-10 Thread paftek

Hi,

I am using SQLAlchemy with PostgreSQL 8.3.
Today I just discovered the great postgres_returning functionnality.

Dumb question :
Is it possible to take advantage of it in session mode ? In other
words, is it possible for session.add() to issue only one INSERT
INTO ... RETURNING ... query instead of two (INSERT then SELECT),
like in the following example :


Code :

class Item(Base):
__tablename__ = 'items'

sku = Column('sku', String(50), primary_key=True)
title = Column('title', String(1000))
foo = Column('foo', Sequence('items_foo_seq'))

def __init__(self, sku, title):
self.sku = sku
self.title = title

item_c = Item('CCC', 'Title CCC')
session.add(item_c)
session.flush()
print item_c.foo


Queries issued :

INSERT INTO items (sku, title, foo) VALUES (E'CCC', E'Title CCC',
nextval('items_foo_seq'))
SELECT items.foo AS items_foo FROM items WHERE items.sku = E'CCC'



Thanks !
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: Filling foreign key with mapping

2007-12-06 Thread paftek

Thanks for these interesting articles.

I was blind ! Why using a small integer surrogate primary key instead
of two chars primary key, why ? =)
As described in the article, I think I am abusing of surrogate primary
keys. Our database schema is not yet finalized, I am going to fix this
right now.

Nevertheless, in some others tables of our schema, I think a surrogate
can not be avoided. It will appear in some others tables and I am
expecting millions of entries. That's why I prefer a small integer
surrogate primary key instead of a 50 or 100 bytes string primary key.

In such cases, I was asking about an elegant way to find the unique
name along the surrogate primary key.

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Filling foreign key with mapping

2007-12-05 Thread paftek

Hi,

Sorry for this meaningless subject !
I am learning SQLAlchemy and I installed version 0.4.1 few days ago.

My problem is probably easy to solve. I swear I have read a good part
of the documentation, and searched this group ! But...

Short example. Two tables :
- languages having a sequence as primary key and a column of unique
short names  ('en', 'fr', etc.)
- items having a foreign key on 'language_id' (see Python code
below)

When creating a new item, it is not significant to fill the foreign
key value with a integer. I wish the mapper had a way to find it
through the unique language name :

item = Item()
item.name = 'A great item'
item.language_name = 'en'
session.save(item)

Please, how to do that ?

Cheers



languages_table = Table('languages', metadata,
Column('language_id', PGInteger, Sequence('language_id_seq'),
primary_key=True),
Column('name', Unicode(), nullable=False, unique=True)
)
items_table = Table('items', metadata,
Column('item_id', PGInteger, Sequence('item_id_seq'),
primary_key=True),
Column('language_id', PGInteger,
ForeignKey('languages.language_id'), primary_key=True),
Column('name', Unicode(), nullable=False)
)
class Item(object): pass

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] SQLAlchemy 0.4.1 example vertical.py not working

2007-12-05 Thread paftek

Using Python 2.5.1, I can not get this example to work :
http://svn.sqlalchemy.org/sqlalchemy/tags/rel_0_4_1/examples/vertical/vertical.py

It crashes with :

Traceback (most recent call last):
  File vertical.py, line 161, in module
session.save(entity3)
 ...
sqlalchemy.exceptions.InvalidRequestError: Instance
'[EMAIL PROTECTED]' is with key (class '__main__.EntityField',
(1,), None) already persisted with a different identity

I do not know why. Any clue ?
Thanks !

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---