Changeset 297

Show
Ignore:
Timestamp:
02/05/08 00:20:01 (5 years ago)
Author:
cleverdevil
Message:

Added a column_format keyword argument to ManyToMany which can be used to specify an alternate format string for column names in the mapping table. This is required to properly integrate with legacy TurboGears identity databases.

Location:
elixir/trunk
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • elixir/trunk/elixir/relationships.py

    r269 r297  
    225225|                    | by a minus (for descending order).                     | 
    226226+--------------------+--------------------------------------------------------+ 
     227| ``column_format``  | Specify an alternate format string for naming the      | 
     228|                    | columns in the mapping table.  The default value is    | 
     229|                    | defined in ``elixir.options.M2MCOL_NAMEFORMAT``.  You  | 
     230|                    | will be passed ``tablename``, ``key``, and ``entity``  | 
     231|                    | as arguments to the format string.                     | 
     232+--------------------+--------------------------------------------------------+ 
     233 
    227234 
    228235================ 
     
    671678        self.ondelete = kwargs.pop('ondelete', None) 
    672679        self.onupdate = kwargs.pop('onupdate', None) 
     680        self.column_format = kwargs.pop('column_format', options.M2MCOL_NAMEFORMAT) 
    673681 
    674682        self.secondary_table = None 
     
    749757             
    750758                for pk_col in desc.primary_keys: 
    751                     colname = options.M2MCOL_NAMEFORMAT % \ 
     759                    colname = self.column_format % \ 
    752760                              {'tablename': desc.tablename, 
    753                                'key': pk_col.key} 
    754  
     761                               'key': pk_col.key, 
     762                               'entity': desc.entity.__name__.lower()} 
     763                     
    755764                    # In case we have a many-to-many self-reference, we  
    756765                    # need to tweak the names of the columns so that we  
  • elixir/trunk/tests/test_m2m.py

    r271 r297  
    1313    def teardown(self): 
    1414        cleanup_all(True) 
    15      
     15         
    1616    def test_simple(self): 
    1717        class A(Entity): 
     
    3535        assert a in b.as_ 
    3636        assert b in a.bs_ 
     37     
     38    def test_column_format(self): 
     39        class A(Entity): 
     40            using_options(tablename='aye') 
     41            name = Field(String(60)) 
     42            bs_ = ManyToMany('B', column_format='%(entity)s_%(key)s') 
    3743 
     44        class B(Entity): 
     45            using_options(tablename='bee') 
     46            name = Field(String(60)) 
     47            as_ = ManyToMany('A', column_format='%(entity)s_%(key)s') 
     48 
     49        setup_all(True) 
     50 
     51        b1 = B(name='b1', as_=[A(name='a1')]) 
     52 
     53        session.flush() 
     54        session.clear() 
     55 
     56        a = A.query.one() 
     57        b = B.query.one() 
     58 
     59        assert a in b.as_ 
     60        assert b in a.bs_ 
     61         
     62        found_a = False 
     63        found_b = False 
     64        for column in A.mapper.get_property('bs_').secondary.columns: 
     65            if column.name == 'a_id': found_a = True 
     66            elif column.name == 'b_id': found_b = True 
     67        assert found_a 
     68        assert found_b 
     69     
    3870    def test_multi_pk_in_target(self): 
    3971        class A(Entity):