Mapping a class to a table with a pk named as one of the class property fails

Issue #1166 resolved
Former user created an issue

(original reporter: ged) It seems like the property overrides the column. Would it be possible (desirable?) to allow for this use case, even if the column is not "renamed" as another property?

The attached test script fails with:

Traceback (most recent call last):
  File "test_pk_prop_sa.py", line 21, in <module>
    m = mapper(Thing, thing_table)
  File "/home/ged/devel/sqlalchemy/trunk/lib/sqlalchemy/orm/__init__.py", line 670, in mapper
    return Mapper(class_, local_table, *args, **params)
  File "/home/ged/devel/sqlalchemy/trunk/lib/sqlalchemy/orm/mapper.py", line 197, in __init__
    self.__compile_pks()
  File "/home/ged/devel/sqlalchemy/trunk/lib/sqlalchemy/orm/mapper.py", line 539, in __compile_pks
    raise sa_exc.ArgumentError("Mapper %s could not assemble any primary key columns for mapped table '%s'" % (self, self.mapped_table.description))
sqlalchemy.exc.ArgumentError: Mapper Mapper|Thing|thing could not assemble any primary key columns for mapped table 'thing'

Comments (6)

  1. Mike Bayer repo owner

    the policy in 0.5 is that end-user descriptors are roughly equivalent to including the column in the "exclude" list. There were some trac tickets that specifically required this policy. So I think elixir is going to have to work around this one.

    1. Figure out the primary key explicitly: m = mapper(Thing, thing_table, primary_key=[thing_table.c.name](thing_table.c.name)).
    2. Remove class descriptors when you first get the class, put them back at the point at which you call add_property()
    3. add placeholder column_properties for primary key columns that you've observed are being overridden by a descriptor
    4. the best approach: don't use add_property() (I don't really see why you need to be using add_property() since you work from a deferred compilation step anyway)
  2. Mike Bayer repo owner

    I think the add_property() call should raise an error if its not going to be honored in the expected manner.

  3. Mike Bayer repo owner

    yeah looking at this again its pretty much a "wontfix" - the PK columns have to be stated up front with the mapper. If you knew the PK cols, this makes it work:

    m = mapper(Thing, thing_table, primary_key=[thing_table.c.name](thing_table.c.name))
    m.add_property('_name', thing_table.c.name)
    
  4. Log in to comment