英文:
AttributeError for sessionmaker
问题
# 导入库
from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.orm import declarative_base
from sqlalchemy.orm import sessionmaker
import psycopg2
# 创建引擎
engine = create_engine('postgresql://postgres:password123@localhost:5432/alchemy', echo=False)
# 创建会话
Session = sessionmaker(bind=engine)
session = Session()
# 创建表
Base = declarative_base()
# 创建继承基类的类
class Student(Base):
__tablename__ = "student"
id = Column(Integer, primary_key=True)
name = Column(String(50))
age = Column(Integer)
grade = Column(String)
# 迁移表到数据库
## Base.metadata.create_all(engine)
# 创建表类的实例
student1 = Student(name="mike", age=32, grade="second")
student2 = Student(name="jenn", age=31, grade="first")
student3 = Student(name="joe", age=23, grade="third")
student4 = Student(name="jack", age=26, grade="fourth")
student5 = Student(name="rose", age=28, grade="fifth")
# 将数据添加到之前创建的会话中
"""在此部分之后,'Base.metadata.create_all(engine)' 行将被注释掉,因为我们在此执行中不需要迁移。"""
session.add_all([student1, student2, student3, student4, student5])
session.commit()
英文:
im new in python and still learning
this is my code :
# import libraries
from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.orm import declarative_base
from sqlalchemy.orm import sessionmaker
import psycopg2
# creating the engine
engine = create_engine('postgresql://postgres:password123@localhost:5432/alchemy', echo=False)
# creating the session
Session = sessionmaker(bind=engine)
session = Session
# creating the table
Base = declarative_base()
# creating the class that inherit the base
class Student(Base):
__tablename__ = "student"
id = Column(Integer, primary_key=True)
name = Column(String(50))
age = Column(Integer)
grade = Column(String)
# migrating table to database
## Base.metadata.create_all(engine)
# create instance os table class
student1 = Student(name="mike", age=32, grade="second")
student2 = Student(name="jenn", age=31, grade="first")
student3 = Student(name="joe", age=23, grade="third")
student4 = Student(name="jack", age=26, grade="forth")
student5 = Student(name="rose", age=28, grade="fifth")
# add data to the session we created before
"""the 'Base.metadata.create_all(engine)' line will transfer to comment after this part because we dont need migrate
in this execution"""
session.add_all()
session.commit()
after running the code i constantly getting this error
line 45, in <module>
session.add_all()
AttributeError: 'sessionmaker' object has no attribute 'add_all'
same error comes for method 'commit()' too
im using pycharm all module and IDE is updated , a light bulb appear near the line and ask for adding this method to the library
sqlalcheamy (2.0.17)
python 3.11
i have checked some other stackoverflow questions, they usually used flask which i dont know what is it , im stil trying to learn sqlalchemy
答案1
得分: 0
session = Session()
你忘了括号。调用 sessionmaker
会返回一个 Session
对象。
同时,使用上下文管理器:
with Session() as session:
session.add(some_object)
session.add(some_other_object)
session.commit()
更多信息,请查看文档:
使用 sessionmaker
英文:
session = Session()
You're missing the parentheses. A call to sessionmaker
returns a Session
object.
Also, use a context manager:
with Session() as session:
session.add(some_object)
session.add(some_other_object)
session.commit()
For more information, check the documentation:
Using a sessionmaker
答案2
得分: 0
上下文管理器只适用于 SQLAlchemy 版本高于1.4。
英文:
Context manager will work only for sqlalchemy higher then 1.4
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论