alembic e SQLite : Erro ao adionar uma nova coluna

13 views
Skip to first unread message

Oberdan Santos

unread,
Nov 10, 2023, 8:18:19 AM11/10/23
to Python Brasil
Olá, todos!!
Estou travado no projeto cookiecutter. Quero  adicionar uma nova coluna  (value2), mas estou encontrando um erro.

# mymodel.py
from sqlalchemy import (
    Column,
    Index,
    Integer,
    Text,
)

from .meta import Base

class MyModel(Base):
    __versioned__ = {}
    __tablename__ = 'models'
    id = Column(Integer, primary_key=True)
    name = Column(Text, nullable=False, unique=True)
    value = Column(Integer, nullable=False)
    value1 = Column(Integer, nullable=False)
    value2 = Column(Integer, nullable=False)

Alembic gera o seguinte script...

"""use new models MyModel

Revision ID: bc850eb0bbba
Revises: 8db6c88f1d4f
Create Date: 2023-11-09 08:58:02.513026

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = 'bc850eb0bbba'
down_revision = '8db6c88f1d4f'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('models', sa.Column('value2', sa.Integer(), nullable=False))
    op.alter_column('models', 'name',
               existing_type=sa.TEXT(),
               nullable=False)
    op.alter_column('models', 'value',
               existing_type=sa.INTEGER(),
               nullable=False)
    op.alter_column('models', 'value1',
               existing_type=sa.INTEGER(),
               nullable=False)
    op.create_unique_constraint(op.f('uq_models_name'), 'models', ['name'])
    # ### end Alembic commands ###

Ao aplicar a migração recebo o seguinte erro.

sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) Cannot add a NOT NULL column with default value NULL
[SQL: ALTER TABLE models ADD COLUMN value2 INTEGER NOT NULL]

Qq. ajuda é bem vinda.

Com os melhores cumprimentos,
Oberdan Costa

























Reply all
Reply to author
Forward
0 new messages