我在MYSQL中使用MyISAM引擎。我发现表中的外键已经自动创建了索引。是Sqlalchemy还是MYSQL的行为?我不需要外键的索引。我该怎么办?定义列时添加参数index = False
我的代码:
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))
我发现B中的a_id索引已自动创建。这是对的吗? 如果我想删除这样的索引,我可以这样做吗?
class B(Base):
__tablename__ = "b"
b_id = Column(BigInteger, primary_key=True, autoincrement=True)
a_id = Column(BigInteger, ForeignKey(A.a_id), index=False)
答案 0 :(得分:1)
我在本地机器上创建了你的模型,但我没有在外键上找到任何索引。
我用
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)
背景日志是。
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
的create table语句。
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
中创建外键索引,则发布您创建的日志和模型,就像我在此答案中所写的那样。
希望这能解决您的问题。
答案 1 :(得分:1)
a_id
(a_id
)清楚地显示了对外键字段的索引。这是InnoDB的行为 - 如果此类索引尚不存在,它将自动在外键关系中的字段上创建索引:
http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html
答案 2 :(得分:0)
这是MySQL的behavior,它对MySQL来说是正确的。你应该同意这个事实。至少我不知道如何摆脱外键的索引。
嗯,也许......我会试试。定义列时添加param index = False?