Multiple issues with Table.append_constraint()

Issue #910 resolved
Former user created an issue

Playing with append_constraints() i noticed various errors, maybe not all grave defects, mostly bad error handling when the user does stupid things, but here they are:

  1. Appending a broken constraint to a table (very degenerated case...) creates a cryptic error message..., should probably tell me that my PrimaryKeyConstraint is broken...

    from sqlalchemy import * meta = MetaData() users_table = Table('users',meta, ... Column('id',Integer,primary_key=True)) ref_table = Table('foo',meta,Column('id_ref',Integer)) pk = PrimaryKeyConstraint('id_ref',users_table.c.id) ref_table.append_constraint(pk) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "c:\sdk\win32\1.0\Python25\lib\site-packages\sqlalchemy-0.4.1-py2.5.egg\s qlalchemy\schema.py", line 270, in append_constraint constraint._set_parent(self) File "c:\sdk\win32\1.0\Python25\lib\site-packages\sqlalchemy-0.4.1-py2.5.egg\s qlalchemy\schema.py", line 902, in _set_parent self.append_column(table.cc) File "c:\sdk\win32\1.0\Python25\lib\site-packages\sqlalchemy-0.4.1-py2.5.egg\s qlalchemy\util.py", line 275, in getitem return self._datakey TypeError: list objects are unhashable

  2. Adding the same constraint more than once does not weed out duplicates, it just adds the ForeignKey() to the Column...

    pk = ForeignKeyConstraint('id_ref',users_table.c.id) ref2_table.append_constraint(pk) ref2_table Table('foo2', MetaData(None), Column('id_ref', Integer(), ForeignKey('users.id') ), Column('name', String(length=30, convert_unicode=False)), schema=None) ref2_table.append_constraint(pk) ref2_table Table('foo2', MetaData(None), Column('id_ref', Integer(), ForeignKey('users.id') , ForeignKey('users.id')), Column('name', String(length=30, convert_unicode=Fals e)), schema=None)

  3. Even worse, you can add different Foreign Keys to the same Column

    pk2 = ForeignKeyConstraint('id_ref',ref_table.c.id_ref) ref2_table.append_constraint(pk2) ref2_table Table('foo2', MetaData(None), Column('id_ref', Integer(), ForeignKey('users.id') , ForeignKey('users.id'), ForeignKey('foo.id_ref')), Column('name', String(lengt h=30, convert_unicode=False)), schema=None)

Comments (5)

  1. Mike Bayer repo owner

    the first two cases are bugs, but #3 is actually a valid use case. a column can have any number of foreign keys placed on it (try it in postgres). im pretty sure we even have a particular ORM test where this condition is part of the schema.

  2. Mike Bayer repo owner

    Element #2 is fixed in 90c572b513fb33cb5cd17134efb6018abc76bb1f. #3 is not a bug, and #1 is not a bug either; Python doesn't supply type signatures for functions and its not feasible for SA to try to solve what's fundamentally a Python behavior across the board. Heres the Python standard lib causing the identical results:

    >>> import logging
    >>> logging.getLogger({'this':'is wrong'})
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/logging/__init__.py", line 1265, in getLogger
        return Logger.manager.getLogger(name)
      File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/logging/__init__.py", line 866, in getLogger
        if self.loggerDict.has_key(name):
    TypeError: dict objects are unhashable
    >>>
    
  3. Log in to comment