sql炼金术会话调用另一个表

时间:2017-10-17 00:35:05

标签: flask-sqlalchemy

class KtOwnerAddressMaster(db.Model):

    __tablename__ = 'kt_owner_address_master'
    ADDRESS_ID = db.Column(Integer, primary_key=True)
    STREET1 = db.Column(String(1024), nullable=False)
    STREET2 = db.Column(String(1024))
    ZIP = db.Column(String(15))
    CITY_NAME = db.Column(String(200))
    STATE = db.Column(String(200))
    COUNTRY = db.Column(String(200))
    ADDR_EXT_ID = db.Column(String(50), nullable=False, unique=True)
    CREATED_ON = db.Column(DateTime, nullable=False)
    UPDATED_ON = db.Column(DateTime)
    def __init__(self, **kwargs):
     super(KtOwnerAddressMaster, self).__init__(**kwargs)
     return
class KtOwnerAddressMap(KtOwnerAddressMaster):
    __tablename__ = 'kt_owner_address_map'
    ADDRESS_ID =db.Column(ForeignKey(u'kt_owner_address_master.ADDRESS_ID'), primary_key=True)
    CURRENT_ADDR = db.Column(SmallInteger, nullable=False, server_default=text("'0'"))
    FRIENDLY_NAME = db.Column(String(100))
    OWNER_ID = db.Column(ForeignKey(u'kt_owner_master.OWNER_ID'), nullable=False, index=True)

    kt_owner_master = relationship(u'KtOwnerMaster')
    def __init__(self, **kwargs):
    super(KtOwnerAddressMap, self).__init__(**kwargs)
    return

在我看来,我正在做以下事情:

    ad_object=KtOwnerAddressMaster(STREET1=street1,
                                   STREET2=street2,
                                   ZIP=zipcode,
                                   ADDR_EXT_ID=addr_ext_id,                          
          CREATED_ON=datetime.now().strftime("%Y:%m:%d %H:%M:%S"),                            
          UPDATED_ON=datetime.now().strftime("%Y:%m:%d %H:%M:%S"),
                                    CITY_NAME =city_name,
                                    STATE=state,
                                    COUNTRY=country)

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

    addr_obj=db.session.query(KtOwnerAddressMaster).filter_by(ADDR_EXT_ID=addr_ext_id).with_entities(KtOwnerAddressMaster.ADDRESS_ID).first()
    address_id=int(addr_obj[0])

现在我正在尝试将地址映射添加到所有者地址映射表。

    add_object=KtOwnerAddressMap(ADDRESS_ID=address_id,
                         CURRENT_ADDR="current_addr",
                         FRIENDLY_NAME="friendly_name",
                         OWNER_ID=owner_id)

    db.session.add(add_object)
    #The error appears on the below commit 
    db.session.commit()

我能够看到数据库中填充的地址。但是我无法在表格中插入所有者到地址映射。 它给了我以下错误: (_mysql_exceptions.OperationalError)(1048,"列' STREET1'不能为空")[SQL:u' INSERT INTO kt_owner_address_master(ADDRESS_IDSTREET1STREET2ZIPCITY_NAMESTATECOUNTRYADDR_EXT_IDCREATED_ONUPDATED_ON)价值观(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)'] [参数:(51,无,无,无,无,无,无,无,无,无)]

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我发现了这个缺陷。我的代码中有一个拼写错误。

    class KtOwnerAddressMap(KtOwnerAddressMaster):

应该是。

    class KtOwnerAddressMap(db.Model):

不知道我最终是怎么做到的。 一切都很好!!