Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从dict保存关系sqlalchemy对象_Python_Postgresql_Sqlalchemy - Fatal编程技术网

Python 从dict保存关系sqlalchemy对象

Python 从dict保存关系sqlalchemy对象,python,postgresql,sqlalchemy,Python,Postgresql,Sqlalchemy,我试图使用sqlalchemy将一个对象保存到postgres数据库,但遇到了问题。下面的代码起作用,但它不是保存位置“Atlanta”的单个实例,而是通过外键引用多个酒店,而是在位置表中反复保存位置 我如何设置我的代码,使我在位置表中有一个条目“Atlanta”,多个酒店引用该位置 以下是我的代码的相关部分: class Hotel(Base): __tablename__ = 'hotels' id = Column(Integer, primary_key=True)

我试图使用sqlalchemy将一个对象保存到postgres数据库,但遇到了问题。下面的代码起作用,但它不是保存位置“Atlanta”的单个实例,而是通过外键引用多个酒店,而是在位置表中反复保存位置

我如何设置我的代码,使我在位置表中有一个条目“Atlanta”,多个酒店引用该位置

以下是我的代码的相关部分:

class Hotel(Base):
    __tablename__ = 'hotels'

    id = Column(Integer, primary_key=True)
    hotelName = Column(String)
    standardRate = Column(Numeric(6, 2))
    govtRate = Column(Numeric(6, 2))
    standardAvailable = Column(Boolean, nullable=False)
    govtAvailable = Column(Boolean, nullable=False)
    arrive = Column(Date, nullable=False)
    depart = Column(Date, nullable=False)
    updated = Column(DateTime, default=datetime.datetime.utcnow, nullable=False)
    location_id = Column(Integer, ForeignKey('location.id'))
    location = relationship('Location')

class Location(Base):
    __tablename__ = 'location'
    id = Column(Integer, primary_key=True)
    city = Column(String, nullable=False)

def scrape(location, arrive, depart, hotelIDs):
    hotels = []
    for id in hotelIDs:
      hotels.append({
            'hotelName': hotelName,
            'standardRate': standardRate,
            'govtRate': govtRate,
            'standardAvailable': standardAvailable,
            'govtAvailable': govtAvailable,
            'arrive': dateToISO(arrive),
            'depart': dateToISO(depart),
            'location': Location(city='Atlanta')
            })
    return hotels

def save_hotel(item):
    session = db_setup()

    hotel = Hotel(**item)
    session.commit()

hotels = scrape("atlanta", "02/20/2016", "02/21/2016", hotelIDs)
for hotel in hotels:
    save_hotel(hotel)

在for语句的每次迭代中,似乎都在创建一个新的
位置
实例:

for id in hotelIDs:
    hotels.append({
        'hotelName': hotelName,
        'standardRate': standardRate,
        'govtRate': govtRate,
        'standardAvailable': standardAvailable,
        'govtAvailable': govtAvailable,
        'arrive': dateToISO(arrive),
        'depart': dateToISO(depart),
        'location': Location(city='Atlanta')  # <- new location instance here
        })
location = Location(city='Atlanta')
# or if you already have Atlanta in your database:
# location = session.query(Location).filter_by(city='Atlanta').first() 

for id in hotelIDs:
    hotel = Hotel( ... )
    location.location.append(hotel)  # append hotel instance here
    ...

# now add to session and commit