我在我的本地機器中創建了您的模型,並且我沒有在外鍵上找到任何索引。
我用
from sqlalchemy import create_engine
engine = create_engine('mysql://test:[email protected]/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)
和背景日誌。
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
我還要檢查在MySQL中創建表的語句a
和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)
索引只在主鍵不在外鍵上。
請在你的機器上試試這個,如果你發現你的機器在表b
的外鍵上創建索引,那麼發佈你創建的日誌和模型就像我在這個答案中寫的那樣。
希望這會解決您的問題。
請提供您的代碼,在MyISAM中創建索引 – Nilesh 2012-02-23 14:08:30
對於沒有索引的東西,您不能擁有外鍵...... – ceejayoz 2012-02-23 15:37:35