loses itself. Affects [ticket:2188].
- schema
+ - Added an informative error message when
+ ForeignKeyConstraint refers to a column name in
+ the parent that is not found. Also in 0.6.9.
+
- Fixed bug whereby adaptation of old append_ddl_listener()
function was passing unexpected **kw through
to the Table event. Table gets no kws, the MetaData
continue
elif base is not cls:
# we're a mixin.
-
if isinstance(obj, Column):
if obj.foreign_keys:
raise exc.InvalidRequestError(
if key == c.key:
del our_stuff[key]
cols = sorted(cols, key=lambda c:c._creation_order)
-
table = None
if '__table__' not in dict_:
if tablename is not None:
# string-specified column names now get
# resolved to Column objects
if isinstance(col, basestring):
- col = table.c[col]
+ try:
+ col = table.c[col]
+ except KeyError:
+ raise exc.ArgumentError(
+ "Can't create ForeignKeyConstraint "
+ "on table '%s': no column "
+ "named '%s' is present." % (table.description, col))
if not hasattr(fk, 'parent') or \
fk.parent is not col:
eq_(Model.__table__.c.keys(), ['col1', 'col3', 'col2', 'col4',
'id'])
+ def test_honor_class_mro_one(self):
+ class HasXMixin(object):
+ @declared_attr
+ def x(self):
+ return Column(Integer)
+
+ class Parent(HasXMixin, Base):
+ __tablename__ = 'parent'
+ id = Column(Integer, primary_key=True)
+
+ class Child(Parent):
+ __tablename__ = 'child'
+ id = Column(Integer, ForeignKey('parent.id'), primary_key=True)
+
+ assert "x" not in Child.__table__.c
+
+ def test_honor_class_mro_two(self):
+ class HasXMixin(object):
+ @declared_attr
+ def x(self):
+ return Column(Integer)
+
+ class Parent(HasXMixin, Base):
+ __tablename__ = 'parent'
+ id = Column(Integer, primary_key=True)
+ def x(self):
+ return "hi"
+
+ class C(Parent):
+ __tablename__ = 'c'
+ id = Column(Integer, ForeignKey('parent.id'), primary_key=True)
+
+ assert C().x() == 'hi'
+
+
class DeclarativeMixinPropertyTest(DeclarativeTestBase):
def test_column_property(self):
fk1 = ForeignKeyConstraint(('foo', ), ('bar', ), table=t1)
assert fk1 in t1.constraints
+ def test_fk_no_such_parent_col_error(self):
+ meta = MetaData()
+ a = Table('a', meta, Column('a', Integer))
+ b = Table('b', meta, Column('b', Integer))
+
+ def go():
+ a.append_constraint(
+ ForeignKeyConstraint(['x'], ['b.b'])
+ )
+ assert_raises_message(
+ exc.ArgumentError,
+ "Can't create ForeignKeyConstraint on "
+ "table 'a': no column named 'x' is present.",
+ go
+ )
+
+ 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
+ )
+
@testing.exclude('mysql', '<', (4, 1, 1), 'early types are squirrely')
def test_to_metadata(self):
meta = MetaData()