Project

General

Profile

« Previous | Next » 

Revision 2681

sql.py: create_table(): Added has_pkey param to disable making the first column the primary key

View differences:

lib/sql.py
873 873
    run_query(db, 'ALTER TABLE '+table+' ADD COLUMN '+row_num_col
874 874
        +' serial NOT NULL PRIMARY KEY', log_level=3)
875 875

  
876
def create_table(db, table, cols):
876
def create_table(db, table, cols, has_pkey=True):
877 877
    '''Creates a table.
878
    The first column becomes the primary key.
879
    @param cols [sql_gen.TypedCol,...] The column names and types.
878
    @param cols [sql_gen.TypedCol,...] The column names and types
879
    @param has_pkey If set, the first column becomes the primary key.
880 880
    '''
881 881
    table = sql_gen.as_Table(table)
882 882
    
883
    pkey = cols.pop(0)
883
    if has_pkey:
884
        cols[0] = pkey = copy.copy(cols[0]) # don't modify input!
885
        pkey.type += ' NOT NULL PRIMARY KEY'
886
    
884 887
    str_ = 'CREATE TABLE '+table.to_str(db)+' (\n'
885
    str_ += '    '+pkey.to_str(db)+' NOT NULL PRIMARY KEY\n'
886
    for col in cols: str_ += '    , '+col.to_str(db)+'\n'
887
    str_ += ');\n'
888
    str_ += '\n, '.join(v.to_str(db) for v in cols)
889
    str_ += '\n);\n'
888 890
    run_query(db, str_, cacheable=True, log_level=2)
889 891

  
890 892
def tables(db, schema_like='public', table_like='%'):

Also available in: Unified diff