如何更改 Alembic 中主键字段的长度?
How to change the length of a Primary Key field in Alembic?
我正在尝试将主键字段的长度从 3 更改为 6。
型号:
class Server(db.Model):
country_code = db.Column(db.String(6), primary_key=True)
迁移:
def upgrade():
op.alter_column('server', 'country_code',
existing_type=mysql.VARCHAR(length=3),
type_=sa.String(length=6))
但是我收到了这个错误消息,我不太明白为什么它认为我正在将它更改为 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')
您需要删除列的主键 属性 才能更改它的数据类型。
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'])
我正在尝试将主键字段的长度从 3 更改为 6。
型号:
class Server(db.Model):
country_code = db.Column(db.String(6), primary_key=True)
迁移:
def upgrade():
op.alter_column('server', 'country_code',
existing_type=mysql.VARCHAR(length=3),
type_=sa.String(length=6))
但是我收到了这个错误消息,我不太明白为什么它认为我正在将它更改为 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')
您需要删除列的主键 属性 才能更改它的数据类型。
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'])