sqlalchemy.orm.exc.FlushError:实例具有NULL身份密钥 [英] sqlalchemy.orm.exc.FlushError: Instance has a NULL identity key

查看:346
本文介绍了sqlalchemy.orm.exc.FlushError:实例具有NULL身份密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很想使用Python27的sqlalchemy将数据提交到mysql表.当我尝试运行此文件时,它显示了这样的错误

I inteded to submit data to mysql table using sqlalchemy from Python27. when I tried to run this file, it shows an error like this

sqlalchemy.orm.exc.FlushError: Instance <TravelScheduleDetailRepository at 0x7f0fc07c8950> 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.

尽管所有列均为空,但它成功地在mysql表上创建了新行

afther that it successfully made a new row on the mysql table although all the columns are null

这是我的课程

class TravelScheduleDetailRepository(Base):
    __tablename__ = 'Schedule_Detail'
    schedule_id = Column(String(7), primary_key = True)
    transport_id = Column(String(5))
    transport_type = Column(String(80))
    transport_company_name = Column(String(80))
    departure_city_id = Column(String(3))
    departure_country_id = Column(String(3))
    destination_city_id = Column(String(3))
    destination_country_id = Column(String(3))
    departure_date = Column(DateTime)
    available_seat = Column(Integer, autoincrement=False)

    def __init__(self, schedule_id, transport_id, transport_type, transport_company_name, departure_city_id, departure_country_id, destination_city_id, destination_country_id, departure_date, available_seat):
            self.schedule_id
            self.transport_id
            self.transport_type
            self.transport_company_name
            self.departure_city_id
            self.departure_country_id
            self.destination_city_id
            self.destination_country_id
            self.departure_date
            self.available_seat

    def __repr__(self):
            return "<TravelScheduleDetailRepository(schedule_id='%s', transport_id='%s', transport_type='%s', transport_company_name='%s', departure_city_id='%s', departure_country_id='%s', destination_city_id='%s', destination_country_id = '%s', departure_date = '%d', available_seat='%i')>" % ( self.schedule_id, self.transport_id, self.transport_type, self.transport_company_name, self.departure_city_id, self.departure_country_id, self.destination_city_id, self.destination_country_id, self.departure_date, self.available_seat)

这是我将其插入mysql的方式

and here's how I insert it to mysql

    Session = sessionmaker(bind=engine)
session = Session()
newSchedule = TravelScheduleDetailRepository(schedule_id='bbb', transport_id='33', transport_type='Hell', transport_company_name='Slick', departure_city_id='GGG', departure_country_id='FOO', destination_city_id='WWW', destination_country_id='FFF', departure_date='03-03-2011', available_seat=40)
session.add(newSchedule)
session.flush()
session.commit()

谢谢

推荐答案

您的__init__方法不完整:要将参数分配给成员变量,您实际上应该assign它们:

Your __init__ method is incomplete: in order to assign parameters into member variables you should actually assign them:

def __init__(...):
    self.schedule_id = schedule_id
    ...

您可以在致电flush之前先致电,只需致电print(newSchedule),您会看到所有字段均为空.

You can call before you call flush, just call print(newSchedule) and you will see that all your fields are empty.

这篇关于sqlalchemy.orm.exc.FlushError:实例具有NULL身份密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆