in the case where there is a multi field PK and a field is marked as autoinc, do... people/jnap/sqlite_multifieldpk_autoinc
John Napiorkowski [Thu, 20 Oct 2011 18:55:44 +0000 (14:55 -0400)]
lib/SQL/Translator/Producer/SQLite.pm

index 1838e5f..2f93d28 100644 (file)
@@ -297,21 +297,26 @@ sub create_field
     my $pk        = $field->table->primary_key;
     my @pk_fields = $pk ? $pk->fields : ();
 
-    if ( 
-         $field->is_primary_key && 
-         scalar @pk_fields == 1 &&
-         (
+    if(
+        $field->is_primary_key && (
           $data_type =~ /int(eger)?$/i
           ||
           ( $data_type =~ /^number?$/i && $size !~ /,/ )
-          )
-         ) {
-        $data_type = 'INTEGER PRIMARY KEY';
-        $size      = undef;
-#        $pk_set    = 1;
+        )
+    ) {
+        if(scalar @pk_fields == 1) {
+            $data_type = 'INTEGER PRIMARY KEY';
+            $size      = undef;
+#           $pk_set    = 1;
+
+        } elsif(scalar(@pk_fields) > 1 && $field->is_auto_increment) {
+            $data_type = 'INTEGER DEFAULT (LAST_INSERT_ROWID())';
+            $size      = undef;
+
+        }
     }
 
-    $field_def .= sprintf " %s%s", $data_type, 
+    $field_def .= sprintf " %s%s", $data_type,
     ( !$field->is_auto_increment && $size ) ? "($size)" : '';
 
     # Null?