Changeset 432 for elixir/trunk
- Timestamp:
- 12/17/08 13:45:49 (3 years ago)
- Location:
- elixir/trunk
- Files:
-
- 2 modified
-
elixir/entity.py (modified) (8 diffs)
-
tests/test_inherit.py (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
elixir/trunk/elixir/entity.py
r428 r432 234 234 # they are added to the parent's table (whether the 235 235 # parent's table is already setup or not). 236 for col in self. columns:236 for col in self._columns: 237 237 self.parent._descriptor.add_column(col) 238 238 for constraint in self.constraints: … … 367 367 # sqlalchemy/test/orm/inheritance/concrete.py 368 368 # this should be added along other 369 kwargs = self.mapper_options369 kwargs = {} 370 370 if self.order_by: 371 371 kwargs['order_by'] = self.translate_order_by(self.order_by) … … 379 379 # non-polymorphic concrete doesn't need this 380 380 kwargs['inherits'] = self.parent.mapper 381 382 if self.inheritance == 'multi' and self.parent:383 col_pairs = zip(self.primary_keys,384 self.parent._descriptor.primary_keys)385 kwargs['inherit_condition'] = \386 and_(*[pc == c for c, pc in col_pairs])387 381 388 382 if self.polymorphic: … … 409 403 self.get_column(self.polymorphic) 410 404 411 #TODO: this is an optimization, and it breaks the multi412 # table polymorphic inheritance test with a relation.413 # So I turn it off for now. We might want to provide an414 # option to turn it on.415 # if self.inheritance == 'multi':416 # children = self._get_children()417 # join = self.entity.table418 # for child in children:419 # join = join.outerjoin(child.table)420 # kwargs['select_table'] = join421 422 405 if self.children or self.parent: 423 406 kwargs['polymorphic_identity'] = self.identity … … 425 408 if self.parent and self.inheritance == 'concrete': 426 409 kwargs['concrete'] = True 410 411 if self.parent and self.inheritance == 'single': 412 args = [] 413 else: 414 args = [self.entity.table] 415 416 # let user-defined kwargs override Elixir-generated ones, though that's 417 # not very usefull since most of them expect Column instances. 418 kwargs.update(self.mapper_options) 427 419 428 420 #TODO: document this! … … 431 423 kwargs['primary_key'] = [getattr(cols, colname) for 432 424 colname in kwargs['primary_key']] 433 434 if self.parent and self.inheritance == 'single':435 args = []436 else:437 args = [self.entity.table]438 425 439 426 # do the mapping … … 596 583 597 584 def columns(self): 598 #FIXME: this would be more correct but it breaks inheritance, so I'll 599 # use the old test for now. 600 # if self.entity.table: 601 if self.autoload: 585 if self.entity.table: 602 586 return self.entity.table.columns 603 587 else: … … 1089 1073 # query methods 1090 1074 def get_by(cls, *args, **kwargs): 1075 """ 1076 Returns the first instance of this class matching the given criteria. 1077 This is equivalent to: 1078 session.query(MyClass).filter_by(...).first() 1079 """ 1091 1080 return cls.query.filter_by(*args, **kwargs).first() 1092 1081 get_by = classmethod(get_by) 1093 1082 1094 1083 def get(cls, *args, **kwargs): 1084 """ 1085 Return the instance of this class based on the given identifier, 1086 or None if not found. This is equivalent to: 1087 session.query(MyClass).get(...) 1088 """ 1095 1089 return cls.query.get(*args, **kwargs) 1096 1090 get = classmethod(get) -
elixir/trunk/tests/test_inherit.py
r424 r432 129 129 130 130 def test_inverse_matching_on_parent(self): 131 options_defaults['inheritance'] = 'multi'132 133 131 class Person(Entity): 134 132 using_options(inheritance='multi') 135 136 133 name = Field(UnicodeText) 137 134 … … 143 140 class Child(Person): 144 141 using_options(inheritance='multi') 145 146 142 parents = ManyToMany('Parent', tablename='child_parent', 147 143 inverse='childs') 148 144 149 145 setup_all() 146 147 def test_multitable_polymorphic_load(self): 148 class A(Entity): 149 using_options(inheritance='multi') 150 # we want to load children's specific data along the parent (A) 151 # data when querying the parent. If we don't specify this, the 152 # children data is loaded lazily 153 using_mapper_options(with_polymorphic='*') 154 name = Field(String(50)) 155 156 class B(A): 157 using_options(inheritance='multi') 158 data = Field(String(50)) 159 some_c = ManyToOne('C') 160 161 class C(A): 162 using_options(inheritance='multi') 163 164 data = Field(String(50)) 165 many_b = OneToMany('B') 166 setup_all(True) 167 a1 = A(name='a1') 168 c1 = C(name='c1', data="c") 169 b1 = B(name='b1', data="b", some_c=c1) 170 171 session.commit() 172 session.clear() 173 174 for a in A.query.all(): 175 if isinstance(a, (B, C)): 176 assert 'data' in a.__dict__ 150 177 151 178 def test_singletable_inheritance(self):
