使用SQLAlchemy将数据批量加载到具有ForeignKey的postgreSQL表中

时间:2017-02-07 18:34:07

标签: postgresql sqlalchemy

我在PostgreSQL / SQLAlchemy中有以下代码。

def load_books():
with open('C:\\Users\\books_raw.csv', 'r') as file:
    for line in file.readlines():
        record = line.split('$') # split at delimiter

        book_isbn = record[0].strip('"')
        book_title = record[1]
        book_authors = record[2]
        book_avg_rating = record[2]
        book_format = record[4]
        book_img_url = record[5]
        book_num_pages = record[6]
        book_pub_date = record[7]
        book_publisher = record[8].strip()  # ESTABLISH RELATIONSHIP

        book = Books(title=book_title, isbn=book_isbn, authors=book_authors, avg_rating=book_avg_rating, format=book_format,
                     img_url=book_img_url, num_pages=book_num_pages, pub_date=book_pub_date, publisher=Publication(name=book_publisher))
        session.add(book)

session.commit()
count = session.query(Books).count()
print(count, ' books added to the database')

我的问题在于这种关系。如果您看到代码的这一部分:

publisher=Publication(name=book_publisher))

这里我不希望将记录插入到表中,而只是与主表中的现有记录建立关系。我有什么想法可以实现这个目标吗?

2 个答案:

答案 0 :(得分:0)

在for循环中检查db中是否为publisher,如果不在db中,则可以创建新的并使用此发布者保存书籍:

for line in file.readlines(): 
    ...
    publisher=Publication.query.filter(name=book_publisher).first()
    if not publisher:
        publisher=Publication(name=book_publisher)
    book = Books(..., publisher=publisher)

答案 1 :(得分:0)

r = session.query(Publication).filter(Publication.name == book_publisher).first()

        book = Books(title=book_title, isbn=book_isbn, authors=book_authors, avg_rating=book_avg_rating, format=book_format,
                     img_url=book_img_url, num_pages=book_num_pages, pub_date=book_pub_date, pub_id=r.id)
        session.add(book)