I created your models in my local machine and i didnt find any index on foreign key.
I use
from sqlalchemy import create_engine
engine = create_engine('mysql://test:test@localhost/test1', echo=True)
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, Date, Text, BigInteger, ForeignKey
from sqlalchemy.orm.attributes import InstrumentedAttribute
Base = declarative_base()
class A(Base):
__tablename__ = "a"
a_id = Column(BigInteger, primary_key=True, autoincrement=True)
class B(Base):
__tablename__ = "b"
b_id = Column(BigInteger, primary_key=True, autoincrement=True)
a_id = Column(BigInteger, ForeignKey(A.a_id))
Base.metadata.create_all(engine)
And the background log is.
2012-02-24 09:39:24,249 INFO sqlalchemy.engine.base.Engine SELECT DATABASE()
2012-02-24 09:39:24,249 INFO sqlalchemy.engine.base.Engine ()
2012-02-24 09:39:24,254 INFO sqlalchemy.engine.base.Engine SHOW VARIABLES LIKE 'character_set%%'
2012-02-24 09:39:24,254 INFO sqlalchemy.engine.base.Engine ()
2012-02-24 09:39:24,277 INFO sqlalchemy.engine.base.Engine SHOW VARIABLES LIKE 'lower_case_table_names'
2012-02-24 09:39:24,277 INFO sqlalchemy.engine.base.Engine ()
2012-02-24 09:39:24,288 INFO sqlalchemy.engine.base.Engine SHOW COLLATION
2012-02-24 09:39:24,289 INFO sqlalchemy.engine.base.Engine ()
2012-02-24 09:39:24,292 INFO sqlalchemy.engine.base.Engine SHOW VARIABLES LIKE 'sql_mode'
2012-02-24 09:39:24,292 INFO sqlalchemy.engine.base.Engine ()
2012-02-24 09:39:24,293 INFO sqlalchemy.engine.base.Engine DESCRIBE `a`
2012-02-24 09:39:24,293 INFO sqlalchemy.engine.base.Engine ()
2012-02-24 09:39:24,321 INFO sqlalchemy.engine.base.Engine ROLLBACK
2012-02-24 09:39:24,322 INFO sqlalchemy.engine.base.Engine DESCRIBE `b`
2012-02-24 09:39:24,322 INFO sqlalchemy.engine.base.Engine ()
2012-02-24 09:39:24,323 INFO sqlalchemy.engine.base.Engine ROLLBACK
2012-02-24 09:39:24,324 INFO sqlalchemy.engine.base.Engine
CREATE TABLE a (
a_id BIGINT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (a_id)
)
2012-02-24 09:39:24,324 INFO sqlalchemy.engine.base.Engine ()
2012-02-24 09:39:24,590 INFO sqlalchemy.engine.base.Engine COMMIT
2012-02-24 09:39:24,591 INFO sqlalchemy.engine.base.Engine
CREATE TABLE b (
b_id BIGINT NOT NULL AUTO_INCREMENT,
a_id BIGINT,
PRIMARY KEY (b_id),
FOREIGN KEY(a_id) REFERENCES a (a_id)
)
2012-02-24 09:39:24,591 INFO sqlalchemy.engine.base.Engine ()
2012-02-24 09:39:24,762 INFO sqlalchemy.engine.base.Engine COMMIT
I also check the create table statement in mysql for a and b.
mysql> show create table a;
+-------+-------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+-------------------------------------------------------------------------------------------------------------------------------+
| a | CREATE TABLE `a` (
`a_id` bigint(20) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`a_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+-------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> show create table b;
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| b | CREATE TABLE `b` (
`b_id` bigint(20) NOT NULL AUTO_INCREMENT,
`a_id` bigint(20) DEFAULT NULL,
PRIMARY KEY (`b_id`),
KEY `a_id` (`a_id`),
CONSTRAINT `b_ibfk_1` FOREIGN KEY (`a_id`) REFERENCES `a` (`a_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
Index is only on primary keys not in foreign key.
Please try this on your machine and if you found that your machine is creating index on foreign key in table b then post that log and model you create like I wrote in this answer .
Hope this will solve your problem.