[sqlalchemy] Re: Filling foreign key with mapping
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] Re: Filling foreign key with mapping
eh, i use surrogate keys all the time :) On Dec 6, 2007, at 4:45 AM, paftek wrote: 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] Re: Filling foreign key with mapping
I believe what you need to do is get an instance of a language object something like: item.Language = session.query(Language).filter_by(name='en').first() There maybe a better way to do this, but its how I currently do it (so if there is a better way I would like to know). paftek wrote: 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 -~--~~~~--~~--~--~---