Пример #1
0
    def test_table_plus_column_exceeds_length(self):
        """test that the truncation occurs if tablename / colname are only
        greater than the max when concatenated."""

        compile_dialect = default.DefaultDialect(label_length=30)
        m = MetaData()
        a_table = Table(
            'thirty_characters_table_xxxxxx',
            m,
            Column('id', Integer, primary_key=True)
        )

        other_table = Table(
            'other_thirty_characters_table_',
            m,
            Column('id', Integer, primary_key=True),
            Column('thirty_characters_table_id',
                Integer,
                ForeignKey('thirty_characters_table_xxxxxx.id'),
                primary_key=True
            )
        )

        anon = a_table.alias()
        self.assert_compile(
            select([other_table,anon]).
                            select_from(
                                other_table.outerjoin(anon)
                        ).apply_labels(),
            "SELECT other_thirty_characters_table_.id AS "
            "other_thirty_characters__1, "
            "other_thirty_characters_table_.thirty_characters_table_id "
            "AS other_thirty_characters__2, thirty_characters_table__1.id "
            "AS thirty_characters_table__3 "
            "FROM other_thirty_characters_table_ "
            "LEFT OUTER JOIN thirty_characters_table_xxxxxx "
            "AS thirty_characters_table__1 ON "
            "thirty_characters_table__1.id = "
            "other_thirty_characters_table_.thirty_characters_table_id",
            dialect=compile_dialect)

        self.assert_compile(
                select([other_table, anon]).
                    select_from(
                                other_table.outerjoin(anon)
                    ).apply_labels(),
            "SELECT other_thirty_characters_table_.id AS "
            "other_thirty_characters__1, "
            "other_thirty_characters_table_.thirty_characters_table_id "
            "AS other_thirty_characters__2, "
            "thirty_characters_table__1.id AS thirty_characters_table__3 "
            "FROM other_thirty_characters_table_ "
            "LEFT OUTER JOIN thirty_characters_table_xxxxxx "
            "AS thirty_characters_table__1 ON "
            "thirty_characters_table__1.id = "
            "other_thirty_characters_table_.thirty_characters_table_id",
            dialect=compile_dialect
        )
Пример #2
0
    def test_table_plus_column_exceeds_length(self):
        """test that the truncation occurs if tablename / colname are only
        greater than the max when concatenated."""

        compile_dialect = default.DefaultDialect(label_length=30)
        m = MetaData()
        a_table = Table('thirty_characters_table_xxxxxx', m,
                        Column('id', Integer, primary_key=True))

        other_table = Table(
            'other_thirty_characters_table_', m,
            Column('id', Integer, primary_key=True),
            Column('thirty_characters_table_id',
                   Integer,
                   ForeignKey('thirty_characters_table_xxxxxx.id'),
                   primary_key=True))

        anon = a_table.alias()
        self.assert_compile(
            select([other_table, anon
                    ]).select_from(other_table.outerjoin(anon)).apply_labels(),
            "SELECT other_thirty_characters_table_.id AS "
            "other_thirty_characters__1, "
            "other_thirty_characters_table_.thirty_characters_table_id "
            "AS other_thirty_characters__2, thirty_characters_table__1.id "
            "AS thirty_characters_table__3 "
            "FROM other_thirty_characters_table_ "
            "LEFT OUTER JOIN thirty_characters_table_xxxxxx "
            "AS thirty_characters_table__1 ON "
            "thirty_characters_table__1.id = "
            "other_thirty_characters_table_.thirty_characters_table_id",
            dialect=compile_dialect)

        self.assert_compile(
            select([other_table, anon
                    ]).select_from(other_table.outerjoin(anon)).apply_labels(),
            "SELECT other_thirty_characters_table_.id AS "
            "other_thirty_characters__1, "
            "other_thirty_characters_table_.thirty_characters_table_id "
            "AS other_thirty_characters__2, "
            "thirty_characters_table__1.id AS thirty_characters_table__3 "
            "FROM other_thirty_characters_table_ "
            "LEFT OUTER JOIN thirty_characters_table_xxxxxx "
            "AS thirty_characters_table__1 ON "
            "thirty_characters_table__1.id = "
            "other_thirty_characters_table_.thirty_characters_table_id",
            dialect=compile_dialect)
    def define_tables(cls, metadata):
        global Table1, Table1B, Table2, Table3,  Data
        table1 = Table('table1', metadata,
                       Column('id', Integer, primary_key=True, test_needs_autoincrement=True),
                       Column('related_id', Integer, ForeignKey('table1.id'), nullable=True),
                       Column('type', String(30)),
                       Column('name', String(30))
                       )

        table2 = Table('table2', metadata,
                       Column('id', Integer, ForeignKey('table1.id'), primary_key=True),
                       )

        table3 = Table('table3', metadata,
                      Column('id', Integer, ForeignKey('table1.id'), primary_key=True),
                      )

        data = Table('data', metadata,
            Column('id', Integer, primary_key=True, test_needs_autoincrement=True),
            Column('node_id', Integer, ForeignKey('table1.id')),
            Column('data', String(30))
            )

        #join = polymorphic_union(
        #    {
        #    'table3' : table1.join(table3),
        #    'table2' : table1.join(table2),
        #    'table1' : table1.select(table1.c.type.in_(['table1', 'table1b'])),
        #    }, None, 'pjoin')

        join = table1.outerjoin(table2).outerjoin(table3).alias('pjoin')
        #join = None

        class Table1(object):
            def __init__(self, name, data=None):
                self.name = name
                if data is not None:
                    self.data = data
            def __repr__(self):
                return "%s(%s, %s, %s)" % (self.__class__.__name__, self.id, repr(str(self.name)), repr(self.data))

        class Table1B(Table1):
            pass

        class Table2(Table1):
            pass

        class Table3(Table1):
            pass

        class Data(object):
            def __init__(self, data):
                self.data = data
            def __repr__(self):
                return "%s(%s, %s)" % (self.__class__.__name__, self.id, repr(str(self.data)))

        try:
            # this is how the mapping used to work.  ensure that this raises an error now
            table1_mapper = mapper(Table1, table1,
                                   select_table=join,
                                   polymorphic_on=table1.c.type,
                                   polymorphic_identity='table1',
                                   properties={
                                    'nxt': relationship(Table1,
                                        backref=backref('prev', foreignkey=join.c.id, uselist=False),
                                        uselist=False, primaryjoin=join.c.id==join.c.related_id),
                                    'data':relationship(mapper(Data, data))
                                    },
                            order_by=table1.c.id)
            configure_mappers()
            assert False
        except:
            assert True
            clear_mappers()

        # currently, the "eager" relationships degrade to lazy relationships
        # due to the polymorphic load.
        # the "nxt" relationship used to have a "lazy='joined'" on it, but the EagerLoader raises the "self-referential"
        # exception now.  since eager loading would never work for that relationship anyway, its better that the user
        # gets an exception instead of it silently not eager loading.
        # NOTE: using "nxt" instead of "next" to avoid 2to3 turning it into __next__() for some reason.
        table1_mapper = mapper(Table1, table1,
                               #select_table=join,
                               polymorphic_on=table1.c.type,
                               polymorphic_identity='table1',
                               properties={
                               'nxt': relationship(Table1,
                                   backref=backref('prev', remote_side=table1.c.id, uselist=False),
                                   uselist=False, primaryjoin=table1.c.id==table1.c.related_id),
                               'data':relationship(mapper(Data, data), lazy='joined', order_by=data.c.id)
                                },
                                order_by=table1.c.id
                        )

        table1b_mapper = mapper(Table1B, inherits=table1_mapper, polymorphic_identity='table1b')

        table2_mapper = mapper(Table2, table2,
                               inherits=table1_mapper,
                               polymorphic_identity='table2')

        table3_mapper = mapper(Table3, table3, inherits=table1_mapper, polymorphic_identity='table3')

        configure_mappers()
        assert table1_mapper.primary_key == (table1.c.id,), table1_mapper.primary_key
Пример #4
0
    def define_tables(cls, metadata):
        global Table1, Table1B, Table2, Table3, Data
        table1 = Table(
            'table1', metadata,
            Column('id',
                   Integer,
                   primary_key=True,
                   test_needs_autoincrement=True),
            Column('related_id',
                   Integer,
                   ForeignKey('table1.id'),
                   nullable=True), Column('type', String(30)),
            Column('name', String(30)))

        table2 = Table(
            'table2',
            metadata,
            Column('id', Integer, ForeignKey('table1.id'), primary_key=True),
        )

        table3 = Table(
            'table3',
            metadata,
            Column('id', Integer, ForeignKey('table1.id'), primary_key=True),
        )

        data = Table(
            'data', metadata,
            Column('id',
                   Integer,
                   primary_key=True,
                   test_needs_autoincrement=True),
            Column('node_id', Integer, ForeignKey('table1.id')),
            Column('data', String(30)))

        #join = polymorphic_union(
        #    {
        #    'table3' : table1.join(table3),
        #    'table2' : table1.join(table2),
        #    'table1' : table1.select(table1.c.type.in_(['table1', 'table1b'])),
        #    }, None, 'pjoin')

        join = table1.outerjoin(table2).outerjoin(table3).alias('pjoin')

        #join = None

        class Table1(object):
            def __init__(self, name, data=None):
                self.name = name
                if data is not None:
                    self.data = data

            def __repr__(self):
                return "%s(%s, %s, %s)" % (self.__class__.__name__, self.id,
                                           repr(str(self.name)), repr(
                                               self.data))

        class Table1B(Table1):
            pass

        class Table2(Table1):
            pass

        class Table3(Table1):
            pass

        class Data(object):
            def __init__(self, data):
                self.data = data

            def __repr__(self):
                return "%s(%s, %s)" % (self.__class__.__name__, self.id,
                                       repr(str(self.data)))

        try:
            # this is how the mapping used to work.  ensure that this raises an error now
            table1_mapper = mapper(
                Table1,
                table1,
                select_table=join,
                polymorphic_on=table1.c.type,
                polymorphic_identity='table1',
                properties={
                    'nxt':
                    relationship(Table1,
                                 backref=backref('prev',
                                                 foreignkey=join.c.id,
                                                 uselist=False),
                                 uselist=False,
                                 primaryjoin=join.c.id == join.c.related_id),
                    'data':
                    relationship(mapper(Data, data))
                },
                order_by=table1.c.id)
            configure_mappers()
            assert False
        except:
            assert True
            clear_mappers()

        # currently, the "eager" relationships degrade to lazy relationships
        # due to the polymorphic load.
        # the "nxt" relationship used to have a "lazy='joined'" on it, but the EagerLoader raises the "self-referential"
        # exception now.  since eager loading would never work for that relationship anyway, its better that the user
        # gets an exception instead of it silently not eager loading.
        # NOTE: using "nxt" instead of "next" to avoid 2to3 turning it into __next__() for some reason.
        table1_mapper = mapper(
            Table1,
            table1,
            #select_table=join,
            polymorphic_on=table1.c.type,
            polymorphic_identity='table1',
            properties={
                'nxt':
                relationship(Table1,
                             backref=backref('prev',
                                             remote_side=table1.c.id,
                                             uselist=False),
                             uselist=False,
                             primaryjoin=table1.c.id == table1.c.related_id),
                'data':
                relationship(mapper(Data, data),
                             lazy='joined',
                             order_by=data.c.id)
            },
            order_by=table1.c.id)

        table1b_mapper = mapper(Table1B,
                                inherits=table1_mapper,
                                polymorphic_identity='table1b')

        table2_mapper = mapper(Table2,
                               table2,
                               inherits=table1_mapper,
                               polymorphic_identity='table2')

        table3_mapper = mapper(Table3,
                               table3,
                               inherits=table1_mapper,
                               polymorphic_identity='table3')

        configure_mappers()
        assert table1_mapper.primary_key == (
            table1.c.id, ), table1_mapper.primary_key