SQLAlchemy: does Column with ForeignKey creates index automatically?

You do need to either specify index=True or create an Index object explicitly:
Index('myindex', mytable.c.col1, mytable.c.col2, unique=True), which allows more control over other parameters of the index, such as the name and support for more than one column.

See Indexes for more information.


As van's answer indicates, you should explicitly add an index as indicated by the docs.

The implementation of foreign keys is database specific, and some DBs such as MySQL will still automatically create an index for foreignkey column, but others will not. See discussion in comments above.

e.g from MySQL docs:

MySQL requires that foreign key columns be indexed; if you create a table with a foreign key constraint but no index on a given column, an index is created.

Tags:

Sqlalchemy