I'm trying to change the length of a Primary Key field from 3 to 6.
Model:
class Server(db.Model):
country_code = db.Column(db.String(6), primary_key=True)
Migration:
def upgrade():
op.alter_column('server', 'country_code',
existing_type=mysql.VARCHAR(length=3),
type_=sa.String(length=6))
However I'm getting this error message, which I don't quite understand, why it thinks that I'm changing it to null.
_mysql_exceptions.DataError: (1171, 'All parts of a PRIMARY KEY must be NOT NULL; if you need NULL in a key, use UNIQUE instead')
You need to remove the primary key property of the column before you can change it's datatype.
def upgrade():
# Drop primary key constraint.
op.execute('ALTER TABLE user DROP PRIMARY KEY')
# Change type of the primary key column.
op.alter_column('server', 'country_code',
existing_type=mysql.VARCHAR(length=3),
type_=sa.String(length=6))
# Re-create the primary key constraint.
op.create_primary_key(None, 'server', ['country_code'])