Sqlalchemy会自动为外键创建索引吗?
我在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)
I use MyISAM engine in MYSQL. I find that the foreign keys in the table has been automatically created indexes. Is the behavior of Sqlalchemy or MYSQL? I don't need the index for the foreign key. What should I do? Add the param index=False when defining the column?
My code:
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))
I found the index for a_id in B has been created automatically. Is it correct?
Can I do this if I want to remove such an index?
class B(Base):
__tablename__ = "b"
b_id = Column(BigInteger, primary_key=True, autoincrement=True)
a_id = Column(BigInteger, ForeignKey(A.a_id), index=False)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
拉法达,你错了。表b中的KEY
a_id
(a_id
)清楚地表明在外键字段上建立了索引。这是 InnoDB 的行为——如果这样的索引尚不存在,它将自动在外键关系中的字段上创建索引:http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html
Lafada, you are incorrect. KEY
a_id
(a_id
) in table b clearly shows an index was made on the foreign key field. This is a behaviour of InnoDB -- it will automatically create an index over fields in a foreign key relationship if such an index does not already exist:http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html
我在本地计算机中创建了您的模型,但没有在外键上找到任何索引。
我用的
是 后台日志是。
我还检查了 mysql 中的 create table 语句的
a
和b
。索引仅在主键上,不在外键上。
请在您的计算机上尝试此操作,如果您发现您的计算机正在表
b
中的外键上创建索引,则发布您创建的日志和模型,就像我在这个答案中所写的那样。希望这能解决您的问题。
I created your models in my local machine and i didnt find any index on foreign key.
I use
And the background log is.
I also check the create table statement in mysql for
a
andb
.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.
这是 MySQL 的行为,它是对于 MySQL 来说是正确的。你应该接受这个事实。至少我不知道如何摆脱外键索引。
嗯,也许...我会尝试一下。
This is MySQL's behavior and it is correct for MySQL. You should reconcile with this fact. At least I don't know how to rid indexing of foreign keys.
Hmm, maybe... I'll try it.