@@ -954,6 +954,15 @@ def create_table(
954954 if transform and self [name ].exists ():
955955 table = cast (Table , self [name ])
956956 should_transform = False
957+ # Has the primary key changed?
958+ current_pks = table .pks
959+ desired_pk = None
960+ if isinstance (pk , str ):
961+ desired_pk = [pk ]
962+ elif pk :
963+ desired_pk = list (pk )
964+ if desired_pk and current_pks != desired_pk :
965+ should_transform = True
957966 # First add missing columns and figure out columns to drop
958967 existing_columns = table .columns_dict
959968 missing_columns = dict (
@@ -964,6 +973,14 @@ def create_table(
964973 columns_to_drop = [
965974 column for column in existing_columns if column not in columns
966975 ]
976+ # If no primary key was specified and id was added automatically, we prevent
977+ # it from being deleted here or Sqlite will complain that a primary key is
978+ # being dropped. We delete the ID column once a new PK has been set.
979+ delete_id_column = False
980+ if table .pks == ['id' ] and 'id' in columns_to_drop :
981+ columns_to_drop .remove ('id' )
982+ delete_id_column = True
983+
967984 if columns_to_drop :
968985 for col_name in columns_to_drop : table .drop_column (col_name )
969986 if missing_columns :
@@ -977,15 +994,6 @@ def create_table(
977994 and list (existing_columns )[: len (column_order )] != column_order
978995 ):
979996 should_transform = True
980- # Has the primary key changed?
981- current_pks = table .pks
982- desired_pk = None
983- if isinstance (pk , str ):
984- desired_pk = [pk ]
985- elif pk :
986- desired_pk = list (pk )
987- if desired_pk and current_pks != desired_pk :
988- should_transform = True
989997 # Any not-null changes?
990998 current_not_null = {c .name for c in table .columns if c .notnull }
991999 desired_not_null = set (not_null ) if not_null else set ()
@@ -1003,6 +1011,9 @@ def create_table(
10031011 defaults = defaults ,
10041012 pk = pk ,
10051013 )
1014+ # Now it is safe to drop the ID column.
1015+ if delete_id_column :
1016+ table .drop_column ('id' )
10061017 return table
10071018 sql = self .create_table_sql (
10081019 name = name ,
0 commit comments