db.session.commit()的时候抛出FlushError:



 class Epg_ps(db.Model):
    __tablename__='epg_ps'
    id = db.Column(db.String(50),primary_key=True)
    pos_left =db.Column(db.Integer)
    pos_top =db.Column(db.Integer)
    pos_width =db.Column(db.Integer)
    pos_height =db.Column(db.Integer)
    epg_type=db.Column(db.Integer,db.ForeignKey('area_type.id'))
    def __repr__(self):
        return '<Epg_ps %s>' % self.id
class Epg_areaType(db.Model):
    __tablename__='area_type'
    id=db.Column(db.Integer,primary_key=True)
    name=db.Column(db.String(50),unique=True)
    type=db.Column(db.Integer,unique=True)
    ps=db.relationship('Epg_ps',backref='type',lazy='dynamic')
    def __repr__(self):
        return '<area_type %>' % self.name
add=Epg_ps(id='regions_1',pos_left=34,pos_top=43,pos_width=43,pos_height=34,epg_type=1)

执行下面的代码的时候就


 db.session.add(add)
db.session.commit()

抛出


 FlushError: Instance <Epg_ps at 0x7ffca0959950> 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.

python flask sqlalchemy

魑魅魍魉X 10 years, 1 month ago

看错误日志:has a NULL identity key
哪个字段赋空值有问题吧。这个检查下

萌也是一种元素 answered 10 years, 1 month ago

最近也在学习SQLAlchemy。
在使用ForeignKey的时候也出错。
我觉得可能是下面这个,add的时候不能直接指定吧


 epg_type=db.Column(db.Integer,db.ForeignKey('area_type.id'))

fafa143 answered 10 years, 1 month ago

Your Answer