sqlalchemy / alembic

A database migrations tool for SQLAlchemy.
MIT License
2.76k stars 241 forks source link

Alembic generate drop_constraint and drop_index on upgrade? #1297

Closed cnicodeme closed 1 year ago

cnicodeme commented 1 year ago

Describe the bug Hi!

I'm facing an odd issue. I found something similar but from 2013 that mentioned Alembic 0.6, but I'm using v1.10.3.

I'm using SQLAlchemy (2.0.9) and have a table like this:

class Article(db.Model):
    __tablename__       = 'kb_articles'
    __table_args__ = (UniqueConstraint('slug', 'knowledge_id', 'code'),)

    id           = Column(Integer, primary_key=True)

    title        = Column(String(250), nullable=False)
    seo_title    = Column(String(250), nullable=False)
    slug         = Column(String(250), nullable=False)
    previous_url = Column(String(250), nullable=True, default=None)

    description  = Column(String(250), nullable=True, default=None)
    content      = Column(MEDIUMTEXT(), nullable=True, default=None)

    created      = Column(DateTime, nullable=False, default=datetime.datetime.utcnow)
    last_updated = Column(DateTime, nullable=False, default=datetime.datetime.utcnow)
    removed      = Column(DateTime, nullable=True, default=None)

    published    = Column('is_published', Boolean, nullable=False, default=False)

    knowledge_id = Column(Integer, ForeignKey('knowledge_bases.id'), nullable=False)
    code         = Column(String(2), nullable=False)

There is a proper constraint with the tuple 'slug', 'knowledge_id', 'code', and the table properly reflects that:

CREATE TABLE `kb_articles` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(250) NOT NULL,
  `slug` varchar(250) NOT NULL,
  `description` varchar(250) DEFAULT NULL,
  `content` mediumtext DEFAULT NULL,
  `created` datetime NOT NULL,
  `last_updated` datetime NOT NULL,
  `removed` datetime DEFAULT NULL,
  `is_published` tinyint(1) NOT NULL,
  `knowledge_id` int(11) NOT NULL,
  `code` varchar(2) NOT NULL,
  `seo_title` varchar(250) NOT NULL,
  `previous_url` varchar(250) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `slug` (`slug`,`knowledge_id`,`code`),
  KEY `knowledge_id` (`knowledge_id`),
  CONSTRAINT `kb_articles_ibfk_1` FOREIGN KEY (`knowledge_id`) REFERENCES `knowledge_bases` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1576 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci

But everytime I run the migrate command, Alembic adds the following:

    op.drop_constraint('slug', 'kb_articles', type_='unique')
    op.drop_index('slug', table_name='kb_articles')

And this is happening for a few tables, not just this one.

Do you have an idea why? Thank you!

Versions.

Thank you !

zzzeek commented 1 year ago

hi -

This reproduces in alembic 1.10.3 and does not reproduce in current released version 1.11.3. This is likely the regression caused by #1166 and now fixed by #1240: https://alembic.sqlalchemy.org/en/latest/changelog.html#change-9e1a6d4ed4cbe0e03d0d55b22313ea41

please upgrade to the latest alembic thanks

cnicodeme commented 1 year ago

Awesome! That was it! Thank you!