2009-09-09 17 views
6

मैं एक बड़े बड़े कोड बेस पर काम कर रहा हूं जिसे sqlalchemy.ext.declarative का उपयोग करके कार्यान्वित किया गया है, और मुझे कक्षाओं में से किसी एक को एक धर्मार्थ संपत्ति जोड़ने की आवश्यकता है। मुझे जो चाहिए वह this question जैसा है, लेकिन एक घोषणात्मक फैशन में है। क्या SQLAlchemy में अधिक ज्ञान वाले किसी को मुझे एक उदाहरण दे सकता है? अग्रिम धन्यवाद ...घोषणात्मक SQLAlchemy में टैग का शब्दकोश?

+0

यदि आपको संपत्ति पर पूछताछ करने की आवश्यकता नहीं है तो यह उत्तर वैकल्पिक दृष्टिकोण प्रदान करता है: http://stackoverflow.com/questions/1378325/python-dicts-in-sqlalchemy/1378818#1378818 –

उत्तर

13

घोषणात्मक चीजों को परिभाषित करने का एक और तरीका है। वस्तुतः आप अलग-अलग वातावरण के साथ समाप्त होते हैं, यदि आपने अलग मैपिंग का उपयोग किया है।

चूंकि मैंने दूसरे प्रश्न का उत्तर दिया, इसलिए मैं इसे भी आजमाउंगा। आशा है कि यह अधिक upvotes देता है;)

ठीक है, पहले हम कक्षाओं

from sqlalchemy import Column, Integer, String, Table, create_engine 
from sqlalchemy import orm, MetaData, Column, ForeignKey 
from sqlalchemy.orm import relation, mapper, sessionmaker 
from sqlalchemy.orm.collections import column_mapped_collection 
from sqlalchemy.ext.associationproxy import association_proxy 
from sqlalchemy.ext.declarative import declarative_base 

engine = create_engine('sqlite:///:memory:', echo=True) 
Base = declarative_base(bind=engine) 

class Note(Base): 
    __tablename__ = 'notes' 

    id_item = Column(Integer, ForeignKey('items.id'), primary_key=True) 
    name = Column(String(20), primary_key=True) 
    value = Column(String(100)) 

    def __init__(self, name, value): 
     self.name = name 
     self.value = value   

class Item(Base): 
    __tablename__ = 'items' 
    id = Column(Integer, primary_key=True) 
    name = Column(String(20)) 
    description = Column(String(100)) 
    _notesdict = relation(Note, 
          collection_class=column_mapped_collection(Note.name)) 
    notes = association_proxy('_notesdict', 'value', creator=Note) 

    def __init__(self, name, description=''): 
     self.name = name 
     self.description = description 

Base.metadata.create_all() 

अब परिभाषित के एक परीक्षण करते हैं:

Session = sessionmaker(bind=engine) 
s = Session() 

i = Item('ball', 'A round full ball') 
i.notes['color'] = 'orange' 
i.notes['size'] = 'big' 
i.notes['data'] = 'none' 

s.add(i) 
s.commit() 
print i.notes 

मैं:

{u'color': u'orange', u'data': u'none', u'size': u'big'} 

आइए अब नोट्स टेबल की जांच करें ...

for note in s.query(Note): 
    print note.id_item, note.name, note.value 

मैं:

1 color orange 
1 data none 
1 size big 

यह काम करता है !! : डी

+0

धन्यवाद @nosklo! –

+0

मुझे एक 'sqlalchemy.exceptions मिल रहा है। कोई संदर्भ नहीं हैटेबल त्रुटि: तालिका' आइटम 'नहीं मिल सका जिसके साथ एक विदेशी कुंजी उत्पन्न करने के लिए –

+0

इसे ठीक किया गया है! 'Note.id_item' से' ForeignKey ('items.id') 'को हटा देना था और 'नोट .__ तालिका __। Append_constraint (ForeignKeyConstraint ([id_item'], ['item.id']), ['item.id']) को जोड़ने के बाद' 'की घोषणा के बाद 'Item'। 'Item.name' में 'नोट .__ तालिका __। C.name' के साथ 'Note.name' को' item._notesdict' में भी बदलना पड़ा। –

संबंधित मुद्दे