zoukankan      html  css  js  c++  java
  • sqlalchemy.orm.exc.flusherror:错误解决

    使用sqlalchemy创建model

    初次代码:

    class UserModel(db.Model):
        __tablename__ = "users"
        id = db.Column(db.String(10),primary_key=True,autoincrement=True)

    改后代码:

    class UserModel(db.Model):
        __tablename__ = "users"
        id = db.Column(db.Integer,primary_key=True,nullable=False,autoincrement=True)

    注:id是字符串类型,无法自增长

    运行,报错如下:

    FlushError: Instance <UserModel at 0x6477550> has a NULL identity key. If this is an auto-generated value, check that the database table allows generation of new primary key values, and that the mapped Column object is configured to expect these generated values. Ensure also that this flush() is not occurring at an inappropriate time, such aswithin a load() event.

    原因:后期修改表字段的时候,不会自动的映射到数据库中,需要重新映射,使用flask-migrate

    app.config.from_object(config)
    db.init_app(app)
    
    #创建命令管理器
    manager = Manager(app)
    # 绑定app到db
    migrate = Migrate(app,db)
    
    manager.add_command('db',MigrateCommand)
    
    if __name__ == "__main__":
        manager.run()

    依次运行:python manage.py db init  、python manage.py db migrate 、python manage.py db upgrade

     注:

    python manage.py db init:初始化一个迁移脚本的环境,只需要执行一次

    python manage.py db migrate`:将模型生成迁移文件,只要模型更改了,就需要执行一遍这个命令。
    python manage.py db upgrade`:将迁移文件真正的映射到数据库中。每次运行了`migrate`命令后,就要运行这个命令

  • 相关阅读:
    UIPasteboard 粘贴板
    UIViewController没有随着设备一起旋转的原因
    UIButton 应用选择状态(附:UIButton 常用状态)
    WebService 中参数为枚举时引发的血案
    设计模式(1)之面向对象设计原则 阿正
    2012年年终总结 阿正
    生活工作如登山 阿正
    感谢我的技术总监 阿正
    尽孝要尽早 阿正
    我老了吗?不 你依然年轻 阿正
  • 原文地址:https://www.cnblogs.com/Ryana/p/8795469.html
Copyright © 2011-2022 走看看