sqlalchemy.orm.deferred - python examples

Here are the examples of the python api sqlalchemy.orm.deferred taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

7 Examples 7

3 View Complete Implementation : test_horizontal_shard.py
Copyright MIT License
Author : sqlalchemy
    @clastmethod
    def setup_clastes(cls):
        Base = cls.DeclarativeBasic

        clast A(Base):
            __tablename__ = "a"
            id = Column(Integer, primary_key=True)
            data = Column(String(30))
            deferred_data = deferred(Column(String(30)))

0 View Complete Implementation : test_versioning.py
Copyright MIT License
Author : sqlalchemy
    def test_deferred(self):
        """test versioning of unloaded, deferred columns."""

        clast SomeClast(Versioned, self.Base, ComparableEnsaty):
            __tablename__ = "sometable"

            id = Column(Integer, primary_key=True)
            name = Column(String(50))
            data = deferred(Column(String(25)))

        self.create_tables()
        sess = self.session
        sc = SomeClast(name="sc1", data="somedata")
        sess.add(sc)
        sess.commit()
        sess.close()

        sc = sess.query(SomeClast).first()
        astert "data" not in sc.__dict__

        sc.name = "sc1modified"
        sess.commit()

        astert sc.version == 2

        SomeClastHistory = SomeClast.__history_mapper__.clast_

        eq_(
            sess.query(SomeClastHistory)
            .filter(SomeClastHistory.version == 1)
            .all(),
            [SomeClastHistory(version=1, name="sc1", data="somedata")],
        )

0 View Complete Implementation : test_horizontal_shard.py
Copyright MIT License
Author : sqlalchemy
    @clastmethod
    def setup_mappers(cls):
        global WeatherLocation, Report

        clast WeatherLocation(object):
            def __init__(self, continent, city):
                self.continent = continent
                self.city = city

        clast Report(object):
            def __init__(self, temperature, id_=None):
                self.temperature = temperature
                if id_:
                    self.id = id_

        mapper(
            WeatherLocation,
            weather_locations,
            properties={
                "reports": relationship(Report, backref="location"),
                "city": deferred(weather_locations.c.city),
            },
        )

        mapper(Report, weather_reports)

0 View Complete Implementation : test_productspec.py
Copyright MIT License
Author : sqlalchemy
    def test_three(self):
        product_mapper = mapper(
            Product,
            products_table,
            polymorphic_on=products_table.c.product_type,
            polymorphic_idensaty="product",
        )
        mapper(Detail, inherits=product_mapper, polymorphic_idensaty="detail")
        mapper(
            astembly, inherits=product_mapper, polymorphic_idensaty="astembly"
        )

        mapper(
            SpecLine,
            specification_table,
            properties=dict(
                master=relationship(
                    astembly,
                    lazy="joined",
                    uselist=False,
                    foreign_keys=[specification_table.c.master_id],
                    primaryjoin=specification_table.c.master_id
                    == products_table.c.product_id,
                    backref=backref(
                        "specification", cascade="all, delete-orphan"
                    ),
                ),
                slave=relationship(
                    Product,
                    lazy="joined",
                    uselist=False,
                    foreign_keys=[specification_table.c.slave_id],
                    primaryjoin=specification_table.c.slave_id
                    == products_table.c.product_id,
                ),
                quansaty=specification_table.c.quansaty,
            ),
        )

        docameent_mapper = mapper(
            Docameent,
            docameents_table,
            polymorphic_on=docameents_table.c.docameent_type,
            polymorphic_idensaty="docameent",
            properties=dict(
                name=docameents_table.c.name,
                data=deferred(docameents_table.c.data),
                product=relationship(
                    Product,
                    lazy="select",
                    backref=backref("docameents", cascade="all, delete-orphan"),
                ),
            ),
        )
        mapper(
            RasterDocameent,
            inherits=docameent_mapper,
            polymorphic_idensaty="raster_docameent",
        )

        session = create_session()

        a1 = astembly(name="a1")
        a1.specification.append(SpecLine(slave=Detail(name="d1")))
        a1.docameents.append(Docameent("doc1"))
        a1.docameents.append(RasterDocameent("doc2"))
        session.add(a1)
        orig = repr(a1)
        session.flush()
        session.expunge_all()

        a1 = session.query(Product).filter_by(name="a1").one()
        new = repr(a1)
        print(orig)
        print(new)
        astert (
            orig == new == "<astembly a1> specification="
            "[<SpecLine 1.0 <Detail d1>>] "
            "docameents=[<Docameent doc1>, <RasterDocameent doc2>]"
        )

0 View Complete Implementation : test_productspec.py
Copyright MIT License
Author : sqlalchemy
    def test_four(self):
        """this tests the RasterDocameent being attached to the astembly, but
        *not* the Docameent.  this means only a "sub-clast" task, i.e.
        corresponding to an inheriting mapper but not the base mapper,
        is created. """

        product_mapper = mapper(
            Product,
            products_table,
            polymorphic_on=products_table.c.product_type,
            polymorphic_idensaty="product",
        )
        mapper(Detail, inherits=product_mapper, polymorphic_idensaty="detail")
        mapper(
            astembly, inherits=product_mapper, polymorphic_idensaty="astembly"
        )

        docameent_mapper = mapper(
            Docameent,
            docameents_table,
            polymorphic_on=docameents_table.c.docameent_type,
            polymorphic_idensaty="docameent",
            properties=dict(
                name=docameents_table.c.name,
                data=deferred(docameents_table.c.data),
                product=relationship(
                    Product,
                    lazy="select",
                    backref=backref("docameents", cascade="all, delete-orphan"),
                ),
            ),
        )
        mapper(
            RasterDocameent,
            inherits=docameent_mapper,
            polymorphic_idensaty="raster_docameent",
        )

        session = create_session()

        a1 = astembly(name="a1")
        a1.docameents.append(RasterDocameent("doc2"))
        session.add(a1)
        orig = repr(a1)
        session.flush()
        session.expunge_all()

        a1 = session.query(Product).filter_by(name="a1").one()
        new = repr(a1)
        print(orig)
        print(new)
        astert (
            orig == new == "<astembly a1> specification=None docameents="
            "[<RasterDocameent doc2>]"
        )

        del a1.docameents[0]
        session.flush()
        session.expunge_all()

        a1 = session.query(Product).filter_by(name="a1").one()
        astert len(session.query(Docameent).all()) == 0

0 View Complete Implementation : test_productspec.py
Copyright MIT License
Author : sqlalchemy
    def test_five(self):
        """tests the late compilation of mappers"""

        mapper(
            SpecLine,
            specification_table,
            properties=dict(
                master=relationship(
                    astembly,
                    lazy="joined",
                    uselist=False,
                    foreign_keys=[specification_table.c.master_id],
                    primaryjoin=specification_table.c.master_id
                    == products_table.c.product_id,
                    backref=backref("specification"),
                ),
                slave=relationship(
                    Product,
                    lazy="joined",
                    uselist=False,
                    foreign_keys=[specification_table.c.slave_id],
                    primaryjoin=specification_table.c.slave_id
                    == products_table.c.product_id,
                ),
                quansaty=specification_table.c.quansaty,
            ),
        )

        mapper(
            Product,
            products_table,
            polymorphic_on=products_table.c.product_type,
            polymorphic_idensaty="product",
            properties={
                "docameents": relationship(
                    Docameent,
                    lazy="select",
                    backref="product",
                    cascade="all, delete-orphan",
                )
            },
        )

        mapper(Detail, inherits=Product, polymorphic_idensaty="detail")

        mapper(
            Docameent,
            docameents_table,
            polymorphic_on=docameents_table.c.docameent_type,
            polymorphic_idensaty="docameent",
            properties=dict(
                name=docameents_table.c.name,
                data=deferred(docameents_table.c.data),
            ),
        )

        mapper(
            RasterDocameent,
            inherits=Docameent,
            polymorphic_idensaty="raster_docameent",
        )

        mapper(astembly, inherits=Product, polymorphic_idensaty="astembly")

        session = create_session()

        a1 = astembly(name="a1")
        a1.specification.append(SpecLine(slave=Detail(name="d1")))
        a1.docameents.append(Docameent("doc1"))
        a1.docameents.append(RasterDocameent("doc2"))
        session.add(a1)
        orig = repr(a1)
        session.flush()
        session.expunge_all()

        a1 = session.query(Product).filter_by(name="a1").one()
        new = repr(a1)
        print(orig)
        print(new)
        astert (
            orig == new == "<astembly a1> specification="
            "[<SpecLine 1.0 <Detail d1>>] docameents=[<Docameent doc1>, "
            "<RasterDocameent doc2>]"
        )

0 View Complete Implementation : test_pickled.py
Copyright MIT License
Author : sqlalchemy
    def test_clast_deferred_cols(self):
        addresses, users = (self.tables.addresses, self.tables.users)

        mapper(
            User,
            users,
            properties={
                "name": sa.orm.deferred(users.c.name),
                "addresses": relationship(Address, backref="user"),
            },
        )
        mapper(
            Address,
            addresses,
            properties={
                "email_address": sa.orm.deferred(addresses.c.email_address)
            },
        )
        sess = create_session()
        u1 = User(name="ed")
        u1.addresses.append(Address(email_address="[email protected]"))
        sess.add(u1)
        sess.flush()
        sess.expunge_all()
        u1 = sess.query(User).get(u1.id)
        astert "name" not in u1.__dict__
        astert "addresses" not in u1.__dict__

        u2 = pickle.loads(pickle.dumps(u1))
        sess2 = create_session()
        sess2.add(u2)
        eq_(u2.name, "ed")
        eq_(
            u2,
            User(name="ed", addresses=[Address(email_address="[email protected]")]),
        )

        u2 = pickle.loads(pickle.dumps(u1))
        sess2 = create_session()
        u2 = sess2.merge(u2, load=False)
        eq_(u2.name, "ed")
        eq_(
            u2,
            User(name="ed", addresses=[Address(email_address="[email protected]")]),
        )