Revision 2686
Added by Aaron Marcuse-Kubitza over 12 years ago
lib/sql.py | ||
---|---|---|
855 | 855 |
+' ('+expr.to_str(db)+')', recover=True, cacheable=True, log_level=3) |
856 | 856 |
except DuplicateTableException: pass # index already existed |
857 | 857 |
|
858 |
def add_pkey(db, table, recover=None): |
|
859 |
'''Makes the first column in a table the primary key. |
|
858 |
def add_pkey(db, table, cols=None, recover=None): |
|
859 |
'''Adds a primary key. |
|
860 |
@param cols [sql_gen.Col,...] The columns in the primary key. |
|
861 |
Defaults to the first column in the table. |
|
860 | 862 |
@pre The table must not already have a primary key. |
861 | 863 |
''' |
862 | 864 |
table = sql_gen.as_Table(table) |
865 |
if cols == None: cols = [pkey(db, table, recover)] |
|
866 |
col_strs = [sql_gen.to_name_only_col(v).to_str(db) for v in cols] |
|
863 | 867 |
|
864 | 868 |
index = sql_gen.as_Table(sql_gen.add_suffix(table.name, '_pkey')) |
865 |
col = sql_gen.to_name_only_col(pkey(db, table, recover)) |
|
866 | 869 |
try: |
867 | 870 |
run_query(db, 'ALTER TABLE '+table.to_str(db)+' ADD CONSTRAINT ' |
868 |
+index.to_str(db)+' PRIMARY KEY('+col.to_str(db)+')',
|
|
871 |
+index.to_str(db)+' PRIMARY KEY ('+(', '.join(col_strs))+')',
|
|
869 | 872 |
recover=True, cacheable=True, log_level=3, |
870 | 873 |
log_ignore_excs=(DuplicateTableException,)) |
871 | 874 |
except DuplicateTableException, e: |
Also available in: Unified diff
sql.py: add_pkey(): Support multiple, custom columns