Форум сайта python.su
Помогите разобраться!
На SQLAlchemy конструирую классику жанра две таблицы Books и Authors причем в одной книги может быть много авторов, и если имя автора уже есть в БД то оно используется.
import os from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import Column, Integer, String, Boolean, ForeignKey from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker, relationship DEFAULT_DIR = '' SQLITE_DB = 'study_sqlalchemy.sqlite' sql_path = "sqlite:///{0}".format(os.path.join(DEFAULT_DIR, SQLITE_DB)) engine = create_engine(sql_path, echo=False) Base = declarative_base() class Books(Base): __tablename__ = 'books' id = Column(Integer, primary_key=True) name = Column(String) author = relationship("Author") class Author(Base): __tablename__ = 'author' id = Column(Integer, primary_key=True) book_id = Column(Integer, ForeignKey('books.id')) name = Column(String) class BookStore: def __init__(self): self.book_name = '' self.authors_name_list = [] self.book = Books() self.authors = [] metadata = Base.metadata metadata.create_all(engine) Session = sessionmaker(bind=engine) self.session = Session() def add(self): self.book.name = self.book_name self.book.author = [] for author_name in self.authors_name_list: author_lst = self.session.query(Author).filter_by(name=author_name) #pdb.set_trace() if not author_lst.count(): author = Author() author.name = author_name else: author = author_lst[0] self.book.author.append(author) if __name__ == '__main__': books = BookStore() books.book_name = 'Book1' books.authors_name_list = ['Author1 Book1', 'Author2 Book1'] books.add()
Офлайн
self.session.commit() ?
Офлайн
plusplusТак тоже ничего. Просто забыл дописать
self.session.commit() ?
books.session.commit()
Офлайн
agrynЯ имел в виду в методе add
Так тоже ничего. Просто забыл дописать
Офлайн
plusplusНу да! Ночь просто была когда я решил разобратся с sqlalchemy
Я имел в виду в методе add
import os from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import Column, Integer, String, Boolean, ForeignKey from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker, relationship DEFAULT_DIR = '' SQLITE_DB = 'study_sqlalchemy.sqlite' sql_uri = "sqlite:///{0}".format(os.path.join(DEFAULT_DIR, SQLITE_DB)) engine = create_engine(sql_uri, echo=False) Base = declarative_base() class Books(Base): __tablename__ = 'books' id = Column(Integer, primary_key=True) name = Column(String) author = relationship("Author") class Author(Base): __tablename__ = 'author' id = Column(Integer, primary_key=True) book_id = Column(Integer, ForeignKey('books.id')) name = Column(String) class BookStore: def __init__(self): self.book_name = '' self.book_authors = [] metadata = Base.metadata metadata.create_all(engine) Session = sessionmaker(bind=engine) self.session = Session() def add(self): book = Books() book.name = self.book_name for author_name in self.book_authors: author_lst = self.session.query(Author).filter_by(name=author_name) #pdb.set_trace() if not author_lst.count(): author = Author() author.name = author_name else: author = author_lst[0] book.author.append(author) self.session.add(book) self.session.commit() self.book_name = '' self.book_authors = [] if __name__ == '__main__': books = BookStore() books.book_name = 'Book1' books.book_authors = ['Author1', 'Author2'] books.add() books.book_name = 'Book2' books.book_authors = ['Author1', 'Author2', 'Author3'] books.add()
Офлайн