Contact: fumanchu@aminus.org

Log in as guest/geniusql to create tickets

Changeset 188

Show
Ignore:
Timestamp:
10/03/07 16:52:11
Author:
fumanchu
Message:

MySQL: Warning and error for manual insertion of 0 into an AUTO_INCREMENT column. Also, raise MappingError? in Database.drop.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/geniusql/providers/mysql.py

    r187 r188  
    378378        else: 
    379379            self.drop_primary() 
    380  
     380     
     381    def insert(self, **kwargs): 
     382        """Insert a row and return it, including any new identifiers.""" 
     383        # MySQL interprets "INSERT INTO x (ID) VALUES (0)" to mean 
     384        # "use the next available number in the sequence" if 
     385        # x is AUTO_INCREMENT. 
     386        for key, col in self.iteritems(): 
     387            if col.autoincrement and kwargs.get(key, None) == 0: 
     388                raise ValueError("MySQL does not allow manually setting an " 
     389                                 "AUTO_INCREMENT column value to 0.") 
     390        return geniusql.Table.insert(self, **kwargs) 
    381391 
    382392 
     
    444454        for colkey, col in table.iteritems(): 
    445455            fields.append(self.columnclause(col)) 
    446              
    447456            if col.autoincrement: 
    448                 # INSERT INTO t (c) VALUES(0) doesn't work for some reason 
    449                 if col.initial > 1: 
     457                if col.initial != 1: 
    450458                    incr_fields.append(col) 
     459                    if col.initial < 1: 
     460                        errors.warn("MySQL interprets manually setting an " 
     461                                    "AUTO_INCREMENT column value to 0 as " 
     462                                    "'use the next available value in the " 
     463                                    "sequence'. By setting %s.initial to %r, " 
     464                                    "there is a slight chance you will " 
     465                                    "encounter this in the future." % 
     466                                    (col.name, col.initial)) 
    451467             
    452468            if col.key: 
     
    669685    def drop(self): 
    670686        conn = self.connections._get_conn(master=True) 
    671         self.execute_ddl('DROP DATABASE %s;' % self.qname, conn) 
    672         conn.close() 
    673  
     687        try: 
     688            try: 
     689                self.execute_ddl('DROP DATABASE %s;' % self.qname, conn) 
     690            except _mysql.OperationalError, x: 
     691                # OperationalError: (1008, "Can't drop database 
     692                # 'dejavu_test'; database doesn't exist") 
     693                if x.args[0] == 1008: 
     694                    raise errors.MappingError(x.args[1]) 
     695        finally: 
     696            conn.close() 
     697