title: How can I add a foreign key constraint on an existing table column via SQLAlchemy?
body: <p>I'm using Flask, Alembic, and PostgreSQL with SQLAlchemy. I have an existing table <code>location_messages</code> with a column <code>campaign_id</code>. This was created initially in the model with the code</p>
  
  <pre><code>campaign_id = db.Column(db.Integer)
  </code></pre>
  
  <p>I want to add a foreign key to it, so I updated the model with</p>
  
  <pre><code>campaign_id = db.Column(db.Integer, db.ForeignKey('campaigns.id'))
  </code></pre>
  
  <p>I ran the <code>revision --autogenerate</code> but it didn't create anything-- so I've been looking through the <a href="http://docs.sqlalchemy.org/en/rel_0_9/core/constraints.html#sqlalchemy.schema.ForeignKeyConstraint" rel="noreferrer">docs</a> but I can't grok the syntax for my usage. For what it's worth, creating the table from scratch (after dropping it) in Alembic is</p>
  
  <pre><code>op.create_table('location_messages',
  [...]
  sa.Column('campaign_id', sa.Integer(), nullable=True),
  sa.ForeignKeyConstraint(['campaign_id'], ['campaigns.id'], ),
  sa.PrimaryKeyConstraint('id')
  )
  </code></pre>
  
  <p>but for my life, I can't find a way to do this for an existing table. Is there a way to just get an instance of a table and assign a <code>ForeignKeyConstraint</code> to a column?</p>