Python sqlalchemy.orm.relationship() Examples

The following are 30 code examples of sqlalchemy.orm.relationship(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module sqlalchemy.orm , or try the search function .
Example #1
Source File: interfaces.py    From jbox with MIT License 6 votes vote down vote up
def _memoized_attr_info(self):
        """Info dictionary associated with the object, allowing user-defined
        data to be associated with this :class:`.InspectionAttr`.

        The dictionary is generated when first accessed.  Alternatively,
        it can be specified as a constructor argument to the
        :func:`.column_property`, :func:`.relationship`, or :func:`.composite`
        functions.

        .. versionadded:: 0.8  Added support for .info to all
           :class:`.MapperProperty` subclasses.

        .. versionchanged:: 1.0.0 :attr:`.MapperProperty.info` is also
           available on extension types via the
           :attr:`.InspectionAttrInfo.info` attribute, so that it can apply
           to a wider variety of ORM and extension constructs.

        .. seealso::

            :attr:`.QueryableAttribute.info`

            :attr:`.SchemaItem.info`

        """
        return {} 
Example #2
Source File: interfaces.py    From Fluid-Designer with GNU General Public License v3.0 6 votes vote down vote up
def _memoized_attr_info(self):
        """Info dictionary associated with the object, allowing user-defined
        data to be associated with this :class:`.InspectionAttr`.

        The dictionary is generated when first accessed.  Alternatively,
        it can be specified as a constructor argument to the
        :func:`.column_property`, :func:`.relationship`, or :func:`.composite`
        functions.

        .. versionadded:: 0.8  Added support for .info to all
           :class:`.MapperProperty` subclasses.

        .. versionchanged:: 1.0.0 :attr:`.MapperProperty.info` is also
           available on extension types via the
           :attr:`.InspectionAttrInfo.info` attribute, so that it can apply
           to a wider variety of ORM and extension constructs.

        .. seealso::

            :attr:`.QueryableAttribute.info`

            :attr:`.SchemaItem.info`

        """
        return {} 
Example #3
Source File: __init__.py    From pyA2L with GNU General Public License v2.0 6 votes vote down vote up
def annotation_association(cls):
        name = cls.__name__
        discriminator = name.lower()

        assoc_cls = type(
            "%sAnnotationAssociation" % name, (AnnotationAssociation,),
            dict(
                __tablename__ = None,
                __mapper_args__ = {"polymorphic_identity": discriminator},
            ),
        )

        cls.annotation = association_proxy(
            "annotation_association",
            "annotation",
            creator = lambda annotation: assoc_cls(annotation = annotation),
        )
        return relationship(
            assoc_cls, backref = backref("parent", uselist = False, collection_class = ordering_list('position'))
        ) 
Example #4
Source File: __init__.py    From pyA2L with GNU General Public License v2.0 5 votes vote down vote up
def phys_unit(cls):
        return relationship("PhysUnit") 
Example #5
Source File: annotation.py    From fonduer with MIT License 5 votes vote down vote up
def candidate(cls) -> relationship:
        """``Candidate``."""
        return relationship(
            "Candidate",
            backref=backref(
                camel_to_under(cls.__name__) + "s",
                cascade="all, delete-orphan",
                cascade_backrefs=False,
            ),
            cascade_backrefs=False,
        ) 
Example #6
Source File: test_serialize.py    From sqlalchemy-jsonapi with MIT License 5 votes vote down vote up
def test_serialize_resource_with_to_many_relationship_success(self):
        """Serailize a resource with a to-many relationship."""

        class UserSerializer(serializer.JSONAPISerializer):
            """Declarative serializer for User."""
            fields = ['id', 'first_name']
            model = self.User

        user = self.User(first_name='Sally')
        self.session.add(user)
        self.session.commit()
        user = self.session.query(self.User).get(user.id)

        user_serializer = UserSerializer()
        serialized_data = user_serializer.serialize(user)

        expected_data = {
            'data': {
                'id': str(user.id),
                'type': user.__tablename__,
                'attributes': {
                    'first-name': u'{}'.format(user.first_name)
                },
                'relationships': {
                    'posts': {
                        'links': {
                            'self': '/users/1/relationships/posts',
                            'related': '/users/1/posts'
                        }
                    }
                }
            },
            'meta': {
                'sqlalchemy_jsonapi_version': '4.0.9'
            },
            'jsonapi': {
                'version': '1.0'
            }
        }
        self.assertEqual(expected_data, serialized_data) 
Example #7
Source File: test_serialize.py    From sqlalchemy-jsonapi with MIT License 5 votes vote down vote up
def test_serialize_resource_with_to_one_relationship_success(self):
        """Serialize a resource with a to-one relationship."""

        class PostSerializer(serializer.JSONAPISerializer):
            """Declarative serializer for Post."""
            fields = ['id', 'title']
            model = self.Post

        blog_post = self.Post(title='Foo')
        self.session.add(blog_post)
        self.session.commit()
        post = self.session.query(self.Post).get(blog_post.id)

        blog_post_serializer = PostSerializer()
        serialized_data = blog_post_serializer.serialize(post)

        expected_data = {
            'data': {
                'id': str(blog_post.id),
                'type': blog_post.__tablename__,
                'attributes': {
                    'title': u'{}'.format(blog_post.title)
                },
                'relationships': {
                    'blog-author': {
                        'links': {
                            'self': '/posts/1/relationships/blog-author',
                            'related': '/posts/1/blog-author'
                        }
                    }
                }
            },
            'meta': {
                'sqlalchemy_jsonapi_version': '4.0.9'
            },
            'jsonapi': {
                'version': '1.0'
            }
        }
        self.assertEqual(expected_data, serialized_data) 
Example #8
Source File: test_serialize.py    From sqlalchemy-jsonapi with MIT License 5 votes vote down vote up
def setUp(self):
        """Configure sqlalchemy and session."""
        self.engine = create_engine('sqlite://')
        Session = sessionmaker(bind=self.engine)
        self.session = Session()
        self.Base = declarative_base()

        class User(self.Base):
            __tablename__ = 'users'
            id = Column(Integer, primary_key=True)
            first_name = Column(String(50), nullable=False)

        class Post(self.Base):
            __tablename__ = 'posts'
            id = Column(Integer, primary_key=True)
            title = Column(String(100), nullable=False)
            author_id = Column(Integer, ForeignKey('users.id',
                                                   ondelete='CASCADE'))

            blog_author = relationship('User',
                                       lazy='joined',
                                       backref=backref('posts',
                                                       lazy='dynamic',
                                                       cascade='all,delete'))

        self.User = User
        self.Post = Post
        self.Base.metadata.create_all(self.engine) 
Example #9
Source File: __init__.py    From pyA2L with GNU General Public License v2.0 5 votes vote down vote up
def symbol_link(cls):
        return relationship("SymbolLink") 
Example #10
Source File: __init__.py    From pyA2L with GNU General Public License v2.0 5 votes vote down vote up
def step_size(cls):
        return relationship("StepSize") 
Example #11
Source File: __init__.py    From pyA2L with GNU General Public License v2.0 5 votes vote down vote up
def ref_unit(cls):
        return relationship("RefUnit") 
Example #12
Source File: __init__.py    From pyA2L with GNU General Public License v2.0 5 votes vote down vote up
def ref_characteristic(cls):
        return relationship("RefCharacteristic") 
Example #13
Source File: __init__.py    From pyA2L with GNU General Public License v2.0 5 votes vote down vote up
def version(cls):
        return relationship("Version") 
Example #14
Source File: __init__.py    From pyA2L with GNU General Public License v2.0 5 votes vote down vote up
def calibration_access(cls):
        return relationship("CalibrationAccess") 
Example #15
Source File: __init__.py    From pyA2L with GNU General Public License v2.0 5 votes vote down vote up
def byte_order(cls):
        return relationship("ByteOrder") 
Example #16
Source File: __init__.py    From pyA2L with GNU General Public License v2.0 5 votes vote down vote up
def bit_mask(cls):
        return relationship("BitMask") 
Example #17
Source File: __init__.py    From pyA2L with GNU General Public License v2.0 5 votes vote down vote up
def alignment_long(cls):
        return relationship("AlignmentLong") 
Example #18
Source File: __init__.py    From pyA2L with GNU General Public License v2.0 5 votes vote down vote up
def alignment_int64(cls):
        return relationship("AlignmentInt64") 
Example #19
Source File: __init__.py    From pyA2L with GNU General Public License v2.0 5 votes vote down vote up
def alignment_float64_ieee(cls):
        return relationship("AlignmentFloat64Ieee") 
Example #20
Source File: __init__.py    From pyA2L with GNU General Public License v2.0 5 votes vote down vote up
def alignment_float32_ieee(cls):
        return relationship("AlignmentFloat32Ieee") 
Example #21
Source File: __init__.py    From pyA2L with GNU General Public License v2.0 5 votes vote down vote up
def alignment_byte(cls):
        return relationship("AlignmentByte") 
Example #22
Source File: __init__.py    From lux with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def assigned(cls):
        return relationship('Person', backref='tasks') 
Example #23
Source File: models.py    From lux with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def user(cls):
        return relationship(
            'User',
            backref=backref("mailinglists", cascade="all, delete-orphan")
        ) 
Example #24
Source File: models.py    From lux with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def user(cls):
        return relationship(
            'User',
            backref=backref("registrations", cascade="all, delete-orphan")
        ) 
Example #25
Source File: models.py    From lux with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def user(cls):
        return relationship(
            'User',
            backref=backref("tokens", cascade="all,delete")) 
Example #26
Source File: models.py    From lux with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def permissions(cls):
        return relationship("Permission",
                            secondary='groups_permissions',
                            backref="groups") 
Example #27
Source File: models.py    From lux with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def users(cls):
        return relationship("User",
                            secondary='users_groups',
                            backref='groups') 
Example #28
Source File: database.py    From dribdat with MIT License 5 votes vote down vote up
def reference_col(tablename, nullable=False, pk_name='id', **kwargs):
    """Column that adds primary key foreign key reference.

    Usage: ::

        category_id = reference_col('category')
        category = relationship('Category', backref='categories')
    """
    return db.Column(
        db.ForeignKey('{0}.{1}'.format(tablename, pk_name)),
        nullable=nullable, **kwargs) 
Example #29
Source File: gateway_device.py    From networking-midonet with Apache License 2.0 5 votes vote down vote up
def _gateway_device_relation(class_name, ref_key):
    relation = "GatewayDevice.id==" + class_name + ".device_id"
    return orm.relationship(
        GatewayDevice,
        backref=orm.backref(ref_key, cascade='delete', lazy='joined'),
        primaryjoin=relation) 
Example #30
Source File: sentence.py    From fonduer with MIT License 5 votes vote down vote up
def cell(cls) -> relationship:
        """Parent ``Cell``, if any."""
        return relationship(
            "Cell",
            backref=backref("sentences", cascade="all, delete-orphan"),
            foreign_keys=lambda: cls.cell_id,
        )