| 181 | | Create primary_key columns. That is, add columns from belongs_to |
| 182 | | relationships marked as being a primary_key and then add a primary |
| 183 | | key to the table if it hasn't already got one and needs one. |
| 184 | | |
| 185 | | This method is "semi-recursive" in that it calls the create_keys |
| 186 | | method on BelongsTo relationships and those in turn call create_pk_cols |
| 187 | | on their target. It shouldn't be possible to have an infinite loop |
| 188 | | since a loop of primary_keys is not a valid situation. |
| | 181 | Create primary_key columns. That is, call the 'create_pk_cols' |
| | 182 | builders then add a primary key to the table if it hasn't already got |
| | 183 | one and needs one. |
| | 184 | |
| | 185 | This method is "semi-recursive" in some cases: it calls the |
| | 186 | create_keys method on ManyToOne relationships and those in turn call |
| | 187 | create_pk_cols on their target. It shouldn't be possible to have an |
| | 188 | infinite loop since a loop of primary_keys is not a valid situation. |
| 198 | | if self.parent: |
| 199 | | if self.inheritance == 'multi': |
| 200 | | # add columns with foreign keys to the parent's primary key |
| 201 | | # columns |
| 202 | | parent_desc = self.parent._descriptor |
| 203 | | for pk_col in parent_desc.primary_keys: |
| 204 | | colname = "%s_%s" % (self.parent.__name__.lower(), |
| 205 | | pk_col.key) |
| 206 | | |
| 207 | | # it seems like SA ForeignKey is not happy being given a |
| 208 | | # real column object when said column is not yet attached |
| 209 | | # to a table |
| 210 | | pk_col_name = "%s.%s" % (parent_desc.tablename, pk_col.key) |
| 211 | | col = Column(colname, pk_col.type, |
| 212 | | ForeignKey(pk_col_name), primary_key=True) |
| 213 | | self.add_column(col) |
| 214 | | elif not self.has_pk and self.auto_primarykey: |
| 215 | | if isinstance(self.auto_primarykey, basestring): |
| 216 | | colname = self.auto_primarykey |
| 217 | | else: |
| 218 | | colname = options.DEFAULT_AUTO_PRIMARYKEY_NAME |
| 219 | | |
| 220 | | self.add_column( |
| 221 | | Column(colname, options.DEFAULT_AUTO_PRIMARYKEY_TYPE, |
| 222 | | primary_key=True)) |
| | 195 | if not self.autoload: |
| | 196 | if self.parent: |
| | 197 | if self.inheritance == 'multi': |
| | 198 | # add columns with foreign keys to the parent's primary |
| | 199 | # key columns |
| | 200 | parent_desc = self.parent._descriptor |
| | 201 | for pk_col in parent_desc.primary_keys: |
| | 202 | colname = "%s_%s" % (self.parent.__name__.lower(), |
| | 203 | pk_col.key) |
| | 204 | |
| | 205 | # it seems like SA ForeignKey is not happy being given |
| | 206 | # a real column object when said column is not yet |
| | 207 | # attached to a table |
| | 208 | pk_col_name = "%s.%s" % (parent_desc.tablename, |
| | 209 | pk_col.key) |
| | 210 | col = Column(colname, pk_col.type, |
| | 211 | ForeignKey(pk_col_name), primary_key=True) |
| | 212 | self.add_column(col) |
| | 213 | elif not self.has_pk and self.auto_primarykey: |
| | 214 | if isinstance(self.auto_primarykey, basestring): |
| | 215 | colname = self.auto_primarykey |
| | 216 | else: |
| | 217 | colname = options.DEFAULT_AUTO_PRIMARYKEY_NAME |
| | 218 | |
| | 219 | self.add_column( |
| | 220 | Column(colname, options.DEFAULT_AUTO_PRIMARYKEY_TYPE, |
| | 221 | primary_key=True)) |