Index: includes/database/pgsql/schema.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/database/pgsql/schema.inc,v
retrieving revision 1.41
diff -u -p -r1.41 schema.inc
--- includes/database/pgsql/schema.inc	14 Dec 2010 19:33:22 -0000	1.41
+++ includes/database/pgsql/schema.inc	26 Dec 2010 14:19:52 -0000
@@ -86,11 +86,16 @@ class DatabaseSchema_pgsql extends Datab
    */
   protected function createTableSql($name, $table) {
     $sql_fields = array();
+    $sql_keys = array();
+
     foreach ($table['fields'] as $field_name => $field) {
-      $sql_fields[] = $this->createFieldSql($field_name, $this->processField($field));
+      $spec = $this->processField($field);
+      $sql_fields[] = $this->createFieldSql($field_name, $spec);
+      if (!empty($spec['unsigned'])) {
+        $sql_keys[] = 'CONSTRAINT ' . $this->prefixNonTable($name, $field_name, 'check') . " CHECK ($field_name >= 0)";
+      }
     }
 
-    $sql_keys = array();
     if (isset($table['primary key']) && is_array($table['primary key'])) {
       $sql_keys[] = 'PRIMARY KEY (' . implode(', ', $table['primary key']) . ')';
     }
@@ -156,10 +161,6 @@ class DatabaseSchema_pgsql extends Datab
       $sql .= '(' . $spec['precision'] . ', ' . $spec['scale'] . ')';
     }
 
-    if (!empty($spec['unsigned'])) {
-      $sql .= " CHECK ($name >= 0)";
-    }
-
     if (isset($spec['not null'])) {
       if ($spec['not null']) {
         $sql .= ' NOT NULL';
@@ -348,6 +349,10 @@ class DatabaseSchema_pgsql extends Datab
     if (!empty($spec['description'])) {
       $this->connection->query('COMMENT ON COLUMN {' . $table . '}.' . $field . ' IS ' . $this->prepareComment($spec['description']));
     }
+
+    if (!empty($spec['unsigned'])) {
+      $this->connection->query('ALTER TABLE {' . $table . '} ADD CONSTRAINT ' . $this->prefixNonTable($table, $field, 'check') . " CHECK ($field >= 0)");
+    }
   }
 
   public function dropField($table, $field) {
@@ -487,6 +492,11 @@ class DatabaseSchema_pgsql extends Datab
       $typecast = $spec['pgsql_type'];
     }
 
+    // Drop a constraint if exists. We will readd it if needed.
+    if ($this->constraintExists($table, $field . '_check')) {
+        $this->connection->query('ALTER TABLE {' . $table . '} DROP CONSTRAINT "' . $this->prefixNonTable($table, $field, 'check') . '"');
+    }
+
     $this->connection->query('ALTER TABLE {' . $table . '} ALTER "' . $field . '" TYPE ' . $typecast . ' USING "' . $field . '"::' . $typecast);
 
     if (isset($spec['not null'])) {
@@ -521,6 +531,10 @@ class DatabaseSchema_pgsql extends Datab
       $this->connection->query('COMMENT ON COLUMN {' . $table . '}."' . $field_new . '" IS ' . $this->prepareComment($spec['description']));
     }
 
+    if (!empty($spec['unsigned'])) {
+      $this->connection->query('ALTER TABLE {' . $table . '} ADD CONSTRAINT ' . $this->prefixNonTable($table, $field_new, 'check') . " CHECK ($field_new >= 0)");
+    }
+
     if (isset($new_keys)) {
       $this->_createKeys($table, $new_keys);
     }

