Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to alter column type from character varying to integer using sqlalchemy-migrate
    primarykey
    data
    text
    <p>I'm using sqlalchemy-migrate to alter the type of one of the columns in a table in a Postgre SQL database. The upgrade script I'm using is:</p> <pre class="lang-python prettyprint-override"><code># -*- cofing: utf-8 -*- from sqlalchemy import MetaData, Table, Column, String, Integer from migrate import changeset metadata = MetaData() def upgrade(migrate_engine): # ALTER TABLE courses ALTER COLUMN number SET DATA TYPE character varying; metadata.bind = migrate_engine courses = Table('courses', metadata, Column("number", Integer), extend_existing=True) courses.c.number.alter(type=String) def downgrade(migrate_engine): # ALTER TABLE courses ALTER COLUMN number SET DATA TYPE integer; metadata.bind = migrate_engine courses = Table('courses', metadata, Column("number", String), extend_existing=True) courses.c.number.alter(type=Integer, cast='numeric') </code></pre> <p>The upgrade part seems to work but the downgrade always fails with the following error:</p> <pre><code>sqlalchemy.exc.ProgrammingError: (ProgrammingError) column "number" cannot be cast to type integer '\nALTER TABLE courses ALTER COLUMN number TYPE INTEGER' {} </code></pre> <p>Now, if I were using plain SQL I could use <code>ALTER TABLE courses ALTER COLUMN number TYPE INTEGER USING number::numeric</code> to alter the column type back from <code>character varying</code> to <code>integer</code>, but I don't know how to achieve that using sqlalchemy-migrate.</p> <p>Is there a way to force sqlalchemy to include <code>USING number::numeric</code> in the <code>ALTER</code> clause? or is there another way to avoid the error I posted above?</p> <p>I appreciate your help.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload