为什么SQLAlchemy为一个外键附加多个约束

时间:2019-11-04 10:16:46

标签: flask sqlalchemy alembic

在Flask项目中,我已经迁移了db,并在运行升级脚本以更新表之后。 升级后的结果给了我重复的外键:

    projectsreal | CREATE TABLE `projectsreal` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `projid` int(11) NOT NULL,
  `memberprj` int(11) DEFAULT NULL,
  `contrib` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `IS_ProjectsReal` (`projid`,`memberprj`),
  **KEY `memberprj` (`memberprj`),
  CONSTRAINT `projectsreal_ibfk_1` FOREIGN KEY (`projid`) REFERENCES `projects` (`projid`) ON DELETE CASCADE,
  CONSTRAINT `projectsreal_ibfk_10` FOREIGN KEY (`memberprj`) REFERENCES `users` (`id`),
  CONSTRAINT `projectsreal_ibfk_11` FOREIGN KEY (`projid`) REFERENCES `projects` (`projid`) ON DELETE CASCADE,
  CONSTRAINT `projectsreal_ibfk_12` FOREIGN KEY (`memberprj`) REFERENCES `users` (`id`)**,...

Alembic给出的脚本修订如下:

    # revision identifiers, used by Alembic.
revision = '5117277c7aa5'
down_revision = '9a67f348194a'

from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import mysql

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    #op.add_column('projectsreal', sa.Column('memberprj', sa.INTEGER(), nullable=True))
    op.create_foreign_key(None, 'projectsreal', 'projects', ['projid'], ['projid'], ondelete='CASCADE')
    op.create_foreign_key(None, 'projectsreal', 'users', ['memberprj'], ['id'])
    op.execute("""CREATE UNIQUE INDEX IS_TaxonomyNameLow ON taxonomy (lower(display_name))""")

是由于op.create_foreign_key(None, 'projectspriv', 'projects', ['projid'], ['projid'], ondelete='CASCADE') 中未命名的ForeignKey吗?

0 个答案:

没有答案