Sqlalchemy会自动为外键创建索引吗?

发布于 2025-01-08 13:17:19 字数 656 浏览 1 评论 0原文

我在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

·深蓝 2025-01-15 13:17:19

拉法达,你错了。表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

家住魔仙堡 2025-01-15 13:17:19

我在本地计算机中创建了您的模型,但没有在外键上找到任何索引。

我用的

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 中的 create table 语句的 ab

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 中的外键上创建索引,则发布您创建的日志和模型,就像我在这个答案中所写的那样。

希望这能解决您的问题。

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.

酸甜透明夹心 2025-01-15 13:17:19

这是 MySQL 的行为,它是对于 MySQL 来说是正确的。你应该接受这个事实。至少我不知道如何摆脱外键索引。

定义列时添加参数index=False?

嗯,也许...我会尝试一下。

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.

Add the param index=False when defining the column?

Hmm, maybe... I'll try it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文