Changeset 421
- Timestamp:
- 12/04/08 15:48:48 (5 years ago)
- Location:
- elixir/trunk/elixir
- Files:
-
- 3 modified
-
entity.py (modified) (4 diffs)
-
options.py (modified) (1 diff)
-
relationships.py (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
-
elixir/trunk/elixir/entity.py
r420 r421 66 66 67 67 # columns and constraints waiting for a table to exist 68 self._columns = list()69 self.constraints = list()68 self._columns = [] 69 self.constraints = [] 70 70 71 71 # properties (it is only useful for checking dupe properties at the 72 72 # moment, and when adding properties before the mapper is created, 73 73 # which shouldn't happen). 74 self.properties = dict()74 self.properties = {} 75 75 76 76 # 77 self.relationships = list()77 self.relationships = [] 78 78 79 79 # set default value for options 80 self.table_args = list()80 self.table_args = [] 81 81 82 82 # base class options_defaults … … 343 343 order_by = [order_by] 344 344 345 order = list()345 order = [] 346 346 for colname in order_by: 347 347 col = self.get_column(colname.strip('-')) … … 373 373 if self.inheritance in ('single', 'concrete', 'multi'): 374 374 if self.parent and \ 375 not (self.inheritance == 'concrete' and not self.polymorphic): 375 (self.inheritance != 'concrete' or self.polymorphic): 376 # non-polymorphic concrete doesn't need this 376 377 kwargs['inherits'] = self.parent.mapper 377 378 … … 388 389 keys.extend([(child._descriptor.identity, child.table) 389 390 for child in self._get_children()]) 390 #XXX: we might need to change the alias name so that 391 # children (which are parent themselves) don't end up 392 # with the same alias than their parent? 391 # Having the same alias name for an entity and one of 392 # its child (which is a parent itself) shouldn't cause 393 # any problem because the join shouldn't be used at 394 # the same time. But in reality, some versions of SA 395 # do misbehave on this. Since it doesn't hurt to have 396 # different names anyway, here they go. 393 397 pjoin = polymorphic_union( 394 dict(keys), self.polymorphic, 'pjoin') 398 dict(keys), self.polymorphic, 399 'pjoin_%s' % self.identity) 395 400 396 401 kwargs['with_polymorphic'] = ('*', pjoin) -
elixir/trunk/elixir/options.py
r417 r421 211 211 allowcoloverride=False, 212 212 order_by=None, 213 mapper_options= dict(),214 table_options= dict()213 mapper_options={}, 214 table_options={} 215 215 ) 216 216 -
elixir/trunk/elixir/relationships.py
r414 r421 414 414 ''' 415 415 416 def __init__(self, of_kind, *args, **kwargs):416 def __init__(self, of_kind, inverse=None, *args, **kwargs): 417 417 super(Relationship, self).__init__() 418 418 419 self.inverse_name = kwargs.pop('inverse', None)420 421 419 self.of_kind = of_kind 420 self.inverse_name = inverse 422 421 423 422 self._target = None … … 542 541 ''' 543 542 544 def __init__(self, *args, **kwargs): 545 self._handle_column_args(kwargs) 546 self._handle_constraint_args(kwargs) 547 548 self.foreign_key = list() 549 self.primaryjoin_clauses = list() 550 551 self.target_column = kwargs.pop('target_column', None) 552 if self.target_column is not None and \ 553 not isinstance(self.target_column, list): 554 self.target_column = [self.target_column] 555 556 super(ManyToOne, self).__init__(*args, **kwargs) 557 558 def _handle_column_args(self, kwargs): 543 def __init__(self, of_kind, 544 column_kwargs=None, 545 colname=None, required=None, primary_key=None, 546 field=None, 547 constraint_kwargs=None, 548 use_alter=None, ondelete=None, onupdate=None, 549 target_column=None, 550 *args, **kwargs): 551 552 # 1) handle column-related args 553 559 554 # check that the column arguments don't conflict 560 col_args_specified = 'column_kwargs' in kwargs or 'colname' in kwargs 561 field_specified = 'field' in kwargs 562 assert not (field_specified and col_args_specified), \ 555 assert not (field and (column_kwargs or colname)), \ 563 556 "ManyToOne can accept the 'field' argument or column " \ 564 557 "arguments ('colname' or 'column_kwargs') but not both!" 565 558 566 self.colname = kwargs.pop('colname', []) 567 if self.colname and not isinstance(self.colname, list): 568 self.colname = [self.colname] 569 570 # populate column_kwargs 571 self.column_kwargs = kwargs.pop('column_kwargs', {}) 572 if 'required' in kwargs: 573 self.column_kwargs['nullable'] = not kwargs.pop('required') 574 if 'primary_key' in kwargs: 575 self.column_kwargs['primary_key'] = kwargs.pop('primary_key') 576 # by default, columns created will have an index. 577 self.column_kwargs.setdefault('index', True) 578 579 self.field = kwargs.pop('field', []) 580 if self.field and not isinstance(self.field, list): 581 self.field = [self.field] 582 583 def _handle_constraint_args(self, kwargs): 584 self.constraint_kwargs = kwargs.pop('constraint_kwargs', {}) 585 if 'use_alter' in kwargs: 586 self.constraint_kwargs['use_alter'] = kwargs.pop('use_alter') 587 588 if 'ondelete' in kwargs: 589 self.constraint_kwargs['ondelete'] = kwargs.pop('ondelete') 590 if 'onupdate' in kwargs: 591 self.constraint_kwargs['onupdate'] = kwargs.pop('onupdate') 559 if colname and not isinstance(colname, list): 560 colname = [colname] 561 self.colname = colname or [] 562 563 column_kwargs = column_kwargs or {} 564 # kwargs go by default to the relation(), so we need to manually 565 # extract those targeting the Column 566 if required is not None: 567 column_kwargs['nullable'] = not required 568 if primary_key is not None: 569 column_kwargs['primary_key'] = primary_key 570 # by default, created columns will have an index. 571 column_kwargs.setdefault('index', True) 572 self.column_kwargs = column_kwargs 573 574 if field and not isinstance(field, list): 575 field = [field] 576 self.field = field or [] 577 578 # 2) handle constraint kwargs 579 constraint_kwargs = constraint_kwargs or {} 580 if use_alter is not None: 581 constraint_kwargs['use_alter'] = use_alter 582 if ondelete is not None: 583 constraint_kwargs['ondelete'] = ondelete 584 if onupdate is not None: 585 constraint_kwargs['onupdate'] = onupdate 586 self.constraint_kwargs = constraint_kwargs 587 588 # 3) misc arguments 589 if target_column and not isinstance(target_column, list): 590 target_column = [target_column] 591 self.target_column = target_column 592 593 self.foreign_key = [] 594 self.primaryjoin_clauses = [] 595 596 super(ManyToOne, self).__init__(of_kind, *args, **kwargs) 592 597 593 598 def match_type_of(self, other): … … 636 641 "relationships.") 637 642 else: 638 fk_refcols = list()639 fk_colnames = list()643 fk_refcols = [] 644 fk_colnames = [] 640 645 641 646 if self.target_column is None: … … 741 746 uselist = False 742 747 743 def __init__(self, *args, **kwargs):744 self.filter = kwargs.pop('filter', None)745 if self.filter is not None:748 def __init__(self, of_kind, filter=None, *args, **kwargs): 749 self.filter = filter 750 if filter is not None: 746 751 # We set viewonly to True by default for filtered relationship, 747 752 # unless manually overridden. … … 757 762 if 'viewonly' not in kwargs: 758 763 kwargs['viewonly'] = True 759 super(OneToOne, self).__init__( *args, **kwargs)764 super(OneToOne, self).__init__(of_kind, *args, **kwargs) 760 765 761 766 def match_type_of(self, other): … … 806 811 uselist = True 807 812 808 def __init__(self, *args, **kwargs): 809 self.user_tablename = kwargs.pop('tablename', None) 810 self.local_colname = kwargs.pop('local_colname', []) 811 if self.local_colname and not isinstance(self.local_colname, list): 812 self.local_colname = [self.local_colname] 813 self.remote_colname = kwargs.pop('remote_colname', []) 814 if self.remote_colname and not isinstance(self.remote_colname, list): 815 self.remote_colname = [self.remote_colname] 816 self.ondelete = kwargs.pop('ondelete', None) 817 self.onupdate = kwargs.pop('onupdate', None) 818 if 'column_format' in kwargs: 813 def __init__(self, of_kind, tablename=None, 814 local_colname=None, remote_colname=None, 815 ondelete=None, onupdate=None, 816 table=None, schema=None, 817 column_format=None, 818 *args, **kwargs): 819 self.user_tablename = tablename 820 821 if local_colname and not isinstance(local_colname, list): 822 local_colname = [local_colname] 823 self.local_colname = local_colname or [] 824 if remote_colname and not isinstance(remote_colname, list): 825 remote_colname = [remote_colname] 826 self.remote_colname = remote_colname or [] 827 828 self.ondelete = ondelete 829 self.onupdate = onupdate 830 831 self.secondary_table = table 832 self.schema = schema 833 834 if column_format: 819 835 warnings.warn("The 'column_format' argument on ManyToMany " 820 836 "relationships is deprecated. Please use the 'local_colname' " … … 823 839 "options.M2MCOL_NAMEFORMAT if you want a custom format for " 824 840 "all ManyToMany tables", DeprecationWarning, stacklevel=3) 825 self.column_format = kwargs.pop('column_format', 826 options.M2MCOL_NAMEFORMAT) 827 828 self.secondary_table = kwargs.pop('table', None) 829 self.schema = kwargs.pop('schema', None) 830 831 self.primaryjoin_clauses = list() 832 self.secondaryjoin_clauses = list() 833 834 super(ManyToMany, self).__init__(*args, **kwargs) 841 self.column_format = column_format or options.M2MCOL_NAMEFORMAT 842 843 self.primaryjoin_clauses = [] 844 self.secondaryjoin_clauses = [] 845 846 super(ManyToMany, self).__init__(of_kind, *args, **kwargs) 835 847 836 848 def match_type_of(self, other): … … 933 945 target_fk_name = "%s_inverse_fk" % source_part 934 946 935 columns = list()936 constraints = list()947 columns = [] 948 constraints = [] 937 949 938 950 joins = (self.primaryjoin_clauses, self.secondaryjoin_clauses) … … 941 953 (1, e2_desc, target_fk_name, self.inverse, self.remote_colname)): 942 954 943 fk_colnames = list()944 fk_refcols = list()955 fk_colnames = [] 956 fk_refcols = [] 945 957 946 958 if colnames: … … 1087 1099 1088 1100 def rel_mutator_handler(target): 1089 def handler(entity, name, *args, **kwargs): 1090 if 'through' in kwargs and 'via' in kwargs: 1101 def handler(entity, name, of_kind=None, through=None, via=None, 1102 *args, **kwargs): 1103 if through and via: 1091 1104 setattr(entity, name, 1092 association_proxy(kwargs.pop('through'), 1093 kwargs.pop('via'), 1094 **kwargs)) 1105 association_proxy(through, via, **kwargs)) 1095 1106 return 1096 elif 'through' in kwargs or 'via' in kwargs:1107 elif through or via: 1097 1108 raise Exception("'through' and 'via' relationship keyword " 1098 1109 "arguments should be used in combination.") 1099 rel = target( kwargs.pop('of_kind'), *args, **kwargs)1110 rel = target(of_kind, *args, **kwargs) 1100 1111 rel.attach(entity, name) 1101 1112 return handler
