ah, if you dont want AUTO_INCREMENT in your create statements, yeah  
that would be a "feature enhancement" at the moment.   nobody has had  
an issue with this since the AUTO_INCREMENT directive doesnt prevent  
you from doing anything except getting an error when no primary key  
is present (and MySQL is generally not big on complaining about  
constraints being violated anyway....)

SQLAlchemy is designed to create tables in such a way that it can  
always generate primary key values without the value being explicitly  
passed;  however, you can always pass an explicit primary key to an  
insert or mapper operation and it will use that value, bypassing the  
AUTO_INCREMENT.  for MySQL, using AUTO_INCREMENT is the only  
(reasonable) way you can get auto-generated PKs, which SA can then  
get a hold of via cursor.lastrowid.

SA does use SERIAL for postgres unless an explicit Sequence or other  
default was specified for the column, although when rows are inserted  
with postgres, and the rows have no explicit PK value present, SA  
will explicitly call the <tablename>_id_seq sequence before each  
INSERT so that it has access to the newly generated primary key value  
(since cursor.lastrowid is somewhat "deprecated" in psycopg).


On Jun 21, 2006, at 7:44 AM, Maciej Szumocki wrote:

> Hello all,
>
>  I'm wondering why the integer primary keys for MySQL are marked
> as AUTO_INCREMENT by default, while they're not marked SERIAL for
> Postgres for example.
>
> I'm referring to this fragment of code in databases\mysql.py:
>
> class MySQLSchemaGenerator(ansisql.ANSISchemaGenerator):
>     def get_column_specification(self, column, override_pk=False,  
> first_pk=False):
>         colspec = column.name + " " + column.type.engine_impl 
> (self.engine).get_col_spec()
>         default = self.get_column_default_string(column)
>         if default is not None:
>             colspec += " DEFAULT " + default
>
>         if not column.nullable:
>             colspec += " NOT NULL"
>         if column.primary_key:
>             if not override_pk:
>                 colspec += " PRIMARY KEY"
>             if not column.foreign_key and first_pk and isinstance 
> (column.type, sqltypes.Integer):
>                 colspec += " AUTO_INCREMENT"
>         if column.foreign_key:
>             colspec += ", FOREIGN KEY (%s) REFERENCES %s(%s)" %  
> (column.name, column.foreign_key.column.table.name,  
> column.foreign_key.column.name)
>         return colspec
>
> Could someone explain the reasoning behind it to me?
>
> (If this is not a bug, could someone please tell me how can I  
> switch this behaviour off? I do not
> want my primary key columns to be auto increment).
>
> Maciej Szumocki
>
>
> _______________________________________________
> Sqlalchemy-users mailing list
> Sqlalchemy-users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users



_______________________________________________
Sqlalchemy-users mailing list
Sqlalchemy-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users

Reply via email to