Wolodja Wentland wrote:
> On Tue, Sep 08, 2009 at 13:06 +0200, Wolodja Wentland wrote:
>> is it possible to drop primary key indexes from within SA? I already
>> found the table.indexes set, but it does not contain the primary key
>> index. I can therefore drop all indexes for a table except the primary
>> key one.
>
> I did some investigation and found that the primary key index is
> generated automagically by PostgreSQL rather than SA. This is because i
> declare some columns with 'primary_key=True' which causes psql to create
> a primary key constraint and an index.
>
> If i connect to the db in question and reflect the tables it is
> unfortunately not possible to drop any pkey constraint, because the
> .drop() method if not present in sa.schema.PrimaryKeyConstraint but only
> in migrate.changeset.constraint.PrimaryKeyConstraint.
>
> I create all PrimaryKeyConstraints explicitly now and use the
> PrimaryKeyConstraint class from migrate. The problem i am facing now is
> that i do not get instances of migrate's PrimaryKeyConstraint but rather
> SA's one if i work on the constraints like exemplified in the following
> code snippet:
>
> --- snip ---
> ...
>
> metadata = MetaData()
> metadata.bind = engine
> self._metadata.reflect()
> tbl = self._metadata.tables[table_name]
>
> pkc = [ con for con in tbl.constraints
>       if isinstance(con, PrimaryKeyConstraint) ][0]
> log.debug('Primary key constraint for table [%s] on: %s'%(
>     table_name, pkc.columns.keys()))
>
> log.debug('Dropping pkey constraint ...')
> pkc.drop()
> ^^^^^^^^^^ This method is not present because i get instances from SA's
> classes not migrate's
> --- snip ---
>
> How can i tackle this problem? Any advise is welcome!

don't rely on Migrate's PKC object except for when you are ready to drop. 
I.e.

for cons in table.constraints:
    if isinstance(con, PrimaryKeyConstraint):
        migrate.PrimaryKeyConstraint(con.name, ...etc...).drop()

Alternatively, just start using SQLalchemy 0.6 (its trunk so far):

from sqlalchemy.schema import DropConstraint
for cons in table.constraints:
    if isinstance(con, PrimaryKeyConstraint):
        engine.execute(DropConstraint(con))


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to