On Thu, Dec 13, 2018 at 2:08 AM Tolstov Sergey <whistler...@gmail.com> wrote: > > Thanks, Not so bad as expected) Can you add this example to official docs?
magic https://docs.sqlalchemy.org/en/latest/orm/composites.html?highlight=composite#nesting-composites > > среда, 12 декабря 2018 г., 20:33:05 UTC+3 пользователь Mike Bayer написал: >> >> composites of composites aren't supported directly so you'd have to >> map out all the columns for "street" directly, then define a >> constructor that handles the columns: >> >> class Address(object): >> def __init__(self, name, value, towndetail): >> self.name = name >> self.value = value >> self.towndetail = towndetail >> >> @classmethod >> def generate(cls, a, b, c, d): >> return Address(a, b, TownDetail(c, d)) >> >> def __composite_values__(self): >> return (self.name, self.value) + self.towndetail.__composite_values__() >> >> def __eq__(self, other): >> return isinstance(other, self.__class__) and\ >> make_dump(self) == make_dump(other) >> >> def __ne__(self, other): >> return not self.__eq__(other) >> >> ... >> >> class TestComposite(Base): >> ... >> towndetail = composite(TownDetail, _street_address_town_detail_name, >> _street_address_town_detail_value) >> street = composite(Address.generate, _street_address_name, >> _street_address_value, _street_address_town_detail_name, >> _street_address_town_detail_value) >> >> that trick using "Address.generate" isn't documented right now. I >> just discovered it :). Another way would be make Address.__init__ >> know how to handle both sets of arguments. >> >> >> On Tue, Dec 11, 2018 at 12:18 AM Tolstov Sergey <whist...@gmail.com> wrote: >> > >> > Can someone have example of this? >> > Example is >> > class TownDetail(object): >> > def __init__(self, name, value): >> > self.name = name >> > self.value = value >> > >> > def __composite_values__(self): >> > return self.name, self.value >> > >> > def __eq__(self, other): >> > return isinstance(other, self.__class__) and\ >> > make_dump(self) == make_dump(other) >> > >> > def __ne__(self, other): >> > return not self.__eq__(other) >> > >> > >> > class Address(object): >> > def __init__(self, name, value, towndetail): >> > self.name = name >> > self.value = value >> > self.towndetail = towndetail >> > >> > def __composite_values__(self): >> > return self.name, self.value >> > >> > def __eq__(self, other): >> > return isinstance(other, self.__class__) and\ >> > make_dump(self) == make_dump(other) >> > >> > def __ne__(self, other): >> > return not self.__eq__(other) >> > >> > class TestComposite(Base): >> > __tablename__ = 'test_composite' >> > id = sqlalchemy.Column(sqlalchemy.types.Integer, primary_key=True) >> > name = sqlalchemy.Column(sqlalchemy.types.String) >> > _street_address_name = sqlalchemy.Column(sqlalchemy.types.String) >> > _street_address_value = sqlalchemy.Column(sqlalchemy.types.Integer) >> > _street_address_town_detail_name = >> > sqlalchemy.Column(sqlalchemy.types.String) >> > _street_address_town_detail_value = >> > sqlalchemy.Column(sqlalchemy.types.Integer) >> > >> > >> > towndetail = composite(TownDetail, _street_address_town_detail_name, >> > _street_address_town_detail_value) >> > street = composite(Address, _street_address_name, _street_address_value, >> > towndetail) >> > >> > >> > test_instance = TestComposite( >> > name='tratata!', street=Address(name=22, value='values', >> > towndetail=TownDetail(322, '322'))) >> > >> > Error is >> > >> > sqlalchemy.exc.ArgumentError: Composite expects Column objects or mapped >> > attributes/attribute names as arguments, got: <CompositeProperty at >> > 0x7f7aa0a75d68; towndetail> >> > >> > have some info in here, but cannot undestand what to do. No access to full >> > example bucket >> > >> > -- >> > SQLAlchemy - >> > The Python SQL Toolkit and Object Relational Mapper >> > >> > http://www.sqlalchemy.org/ >> > >> > To post example code, please provide an MCVE: Minimal, Complete, and >> > Verifiable Example. See http://stackoverflow.com/help/mcve for a full >> > description. >> > --- >> > You received this message because you are subscribed to the Google Groups >> > "sqlalchemy" group. >> > To unsubscribe from this group and stop receiving emails from it, send an >> > email to sqlalchemy+...@googlegroups.com. >> > To post to this group, send email to sqlal...@googlegroups.com. >> > Visit this group at https://groups.google.com/group/sqlalchemy. >> > For more options, visit https://groups.google.com/d/optout. > > -- > SQLAlchemy - > The Python SQL Toolkit and Object Relational Mapper > > http://www.sqlalchemy.org/ > > To post example code, please provide an MCVE: Minimal, Complete, and > Verifiable Example. See http://stackoverflow.com/help/mcve for a full > description. > --- > You received this message because you are subscribed to the Google Groups > "sqlalchemy" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to sqlalchemy+unsubscr...@googlegroups.com. > To post to this group, send email to sqlalchemy@googlegroups.com. > Visit this group at https://groups.google.com/group/sqlalchemy. > For more options, visit https://groups.google.com/d/optout. -- SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper http://www.sqlalchemy.org/ To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description. --- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at https://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.