| 164 | | if col_type is not None: |
|---|
| 165 | | # Make the definition (e.g. 'foo VARCHAR(30)') for this field. |
|---|
| 166 | | field_output = [style.SQL_FIELD(backend.quote_name(f.column)), |
|---|
| 167 | | style.SQL_COLTYPE(col_type)] |
|---|
| 168 | | field_output.append(style.SQL_KEYWORD('%sNULL' % (not f.null and 'NOT ' or ''))) |
|---|
| 169 | | if f.unique and (not f.primary_key or backend.allows_unique_and_pk): |
|---|
| 170 | | field_output.append(style.SQL_KEYWORD('UNIQUE')) |
|---|
| 171 | | if f.primary_key: |
|---|
| 172 | | field_output.append(style.SQL_KEYWORD('PRIMARY KEY')) |
|---|
| 173 | | if tablespace and backend.supports_tablespaces and (f.unique or f.primary_key) and backend.autoindexes_primary_keys: |
|---|
| 174 | | # We must specify the index tablespace inline, because we |
|---|
| 175 | | # won't be generating a CREATE INDEX statement for this field. |
|---|
| 176 | | field_output.append(backend.get_tablespace_sql(tablespace, inline=True)) |
|---|
| 177 | | if f.rel: |
|---|
| 178 | | if f.rel.to in known_models: |
|---|
| 179 | | field_output.append(style.SQL_KEYWORD('REFERENCES') + ' ' + \ |
|---|
| 180 | | style.SQL_TABLE(backend.quote_name(f.rel.to._meta.db_table)) + ' (' + \ |
|---|
| 181 | | style.SQL_FIELD(backend.quote_name(f.rel.to._meta.get_field(f.rel.field_name).column)) + ')' + |
|---|
| 182 | | backend.get_deferrable_sql() |
|---|
| 183 | | ) |
|---|
| 184 | | else: |
|---|
| 185 | | # We haven't yet created the table to which this field |
|---|
| 186 | | # is related, so save it for later. |
|---|
| 187 | | pr = pending_references.setdefault(f.rel.to, []).append((model, f)) |
|---|
| 188 | | table_output.append(' '.join(field_output)) |
|---|
| | 164 | if col_type is None: |
|---|
| | 165 | # Skip ManyToManyFields, because they're not represented as |
|---|
| | 166 | # database columns in this table. |
|---|
| | 167 | continue |
|---|
| | 168 | # Make the definition (e.g. 'foo VARCHAR(30)') for this field. |
|---|
| | 169 | field_output = [style.SQL_FIELD(backend.quote_name(f.column)), |
|---|
| | 170 | style.SQL_COLTYPE(col_type)] |
|---|
| | 171 | field_output.append(style.SQL_KEYWORD('%sNULL' % (not f.null and 'NOT ' or ''))) |
|---|
| | 172 | if f.unique and (not f.primary_key or backend.allows_unique_and_pk): |
|---|
| | 173 | field_output.append(style.SQL_KEYWORD('UNIQUE')) |
|---|
| | 174 | if f.primary_key: |
|---|
| | 175 | field_output.append(style.SQL_KEYWORD('PRIMARY KEY')) |
|---|
| | 176 | if tablespace and backend.supports_tablespaces and (f.unique or f.primary_key) and backend.autoindexes_primary_keys: |
|---|
| | 177 | # We must specify the index tablespace inline, because we |
|---|
| | 178 | # won't be generating a CREATE INDEX statement for this field. |
|---|
| | 179 | field_output.append(backend.get_tablespace_sql(tablespace, inline=True)) |
|---|
| | 180 | if f.rel: |
|---|
| | 181 | if f.rel.to in known_models: |
|---|
| | 182 | field_output.append(style.SQL_KEYWORD('REFERENCES') + ' ' + \ |
|---|
| | 183 | style.SQL_TABLE(backend.quote_name(f.rel.to._meta.db_table)) + ' (' + \ |
|---|
| | 184 | style.SQL_FIELD(backend.quote_name(f.rel.to._meta.get_field(f.rel.field_name).column)) + ')' + |
|---|
| | 185 | backend.get_deferrable_sql() |
|---|
| | 186 | ) |
|---|
| | 187 | else: |
|---|
| | 188 | # We haven't yet created the table to which this field |
|---|
| | 189 | # is related, so save it for later. |
|---|
| | 190 | pr = pending_references.setdefault(f.rel.to, []).append((model, f)) |
|---|
| | 191 | table_output.append(' '.join(field_output)) |
|---|