Exemplo n.º 1
0
    def test_append_constraint_unique(self):
        meta = MetaData()

        users = Table('users', meta, Column('id', sa.Integer))
        addresses = Table('addresses', meta, Column('id', sa.Integer), Column('user_id', sa.Integer))

        fk = sa.ForeignKeyConstraint(['user_id'],[users.c.id])

        addresses.append_constraint(fk)
        addresses.append_constraint(fk)
        assert len(addresses.c.user_id.foreign_keys) == 1
        assert addresses.constraints == set([addresses.primary_key, fk])
Exemplo n.º 2
0
    def test_append_constraint_unique(self):
        meta = MetaData()

        users = Table('users', meta, Column('id', sa.Integer))
        addresses = Table('addresses', meta, Column('id', sa.Integer),
                          Column('user_id', sa.Integer))

        fk = sa.ForeignKeyConstraint(['user_id'], [users.c.id])

        addresses.append_constraint(fk)
        addresses.append_constraint(fk)
        assert len(addresses.c.user_id.foreign_keys) == 1
        assert addresses.constraints == set([addresses.primary_key, fk])
Exemplo n.º 3
0
    def test_fk_no_such_target_col_error(self):
        meta = MetaData()
        a = Table('a', meta, Column('a', Integer))
        b = Table('b', meta, Column('b', Integer))
        a.append_constraint(ForeignKeyConstraint(['a'], ['b.x']))

        def go():
            list(a.c.a.foreign_keys)[0].column

        assert_raises_message(
            exc.NoReferencedColumnError,
            "Could not create ForeignKey 'b.x' on "
            "table 'a': table 'b' has no column named 'x'", go)
Exemplo n.º 4
0
    def test_fk_no_such_target_col_error(self):
        meta = MetaData()
        a = Table('a', meta, Column('a', Integer))
        b = Table('b', meta, Column('b', Integer))
        a.append_constraint(
            ForeignKeyConstraint(['a'], ['b.x'])
        )

        def go():
            list(a.c.a.foreign_keys)[0].column
        assert_raises_message(
            exc.NoReferencedColumnError,
            "Could not create ForeignKey 'b.x' on "
            "table 'a': table 'b' has no column named 'x'",
            go
        )
Exemplo n.º 5
0
 def test_fk_copy(self):
     c1 = Column('foo', Integer)
     c2 = Column('bar', Integer)
     m = MetaData()
     t1 = Table('t', m, c1, c2)
     
     kw = dict(onupdate="X", 
                     ondelete="Y", use_alter=True, name='f1',
                     deferrable="Z", initially="Q", link_to_name=True)
                     
     fk1 = ForeignKey(c1, **kw) 
     fk2 = ForeignKeyConstraint((c1,), (c2,), **kw)
     
     t1.append_constraint(fk2)
     fk1c = fk1.copy()
     fk2c = fk2.copy()
     
     for k in kw:
         eq_(getattr(fk1c, k), kw[k])
         eq_(getattr(fk2c, k), kw[k])
Exemplo n.º 6
0
    def test_fk_copy(self):
        c1 = Column('foo', Integer)
        c2 = Column('bar', Integer)
        m = MetaData()
        t1 = Table('t', m, c1, c2)

        kw = dict(onupdate="X",
                  ondelete="Y",
                  use_alter=True,
                  name='f1',
                  deferrable="Z",
                  initially="Q",
                  link_to_name=True)

        fk1 = ForeignKey(c1, **kw)
        fk2 = ForeignKeyConstraint((c1, ), (c2, ), **kw)

        t1.append_constraint(fk2)
        fk1c = fk1.copy()
        fk2c = fk2.copy()

        for k in kw:
            eq_(getattr(fk1c, k), kw[k])
            eq_(getattr(fk2c, k), kw[k])