我试图通过pyodbc和SQL Server与sqlalchemy建立多对多的关系,这是我的代码:
from sqlalchemy import Table, Text
from sqlalchemy import ForeignKey
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
courses_students = Table('courses_students',Base.metadata,
Column('stu_id',ForeignKey('student.id'),primary_key=True),
Column('cour_id',ForeignKey('course.id'),primary_key=True),
Column('grade',Integer())
)
# from sqlalchemy.ext.declarative import declarative_base
# Base = declarative_base()
class Student(Base):
__tablename__ = 'student'
stu_id = Column('id',Integer,primary_key=True)
name = Column('name',String(10))
stu_class = Column('class',String(10))
#courses = relationship('Course',secondary=courses_students,back_populates='students')
def __init__(self,stu_id,name,stu_class):
self.stu_id = stu_id
self.name = name
self.stu_class = stu_class
def __repr__(self):
_str = "Student({_id},{_name},{_class})".format(_id=self.stu_id,_name=self.name,
_class=self.stu_class)
return _str
# from sqlalchemy.ext.declarative import declarative_base
# Base = declarative_base()
class Course(Base):
__tablename__ = 'course'
cour_id = Column('id',Integer,primary_key=True)
name = Column('name',String(20))
teacher_name = Column('teacher_name',String(10))
credit = Column('credit',Integer())
students = relationship('Student',secondary=courses_students,backref='courses')
def __init__(self,c_id,name,t_name,credit):
self.cour_id = c_id
self.name = name
self.teacher_name = t_name
self.credit = credit
def __repr__(self):
_str = "Course({_id},{_na},{_t_na},{_cre})".format(_id=self.cour_id,_na=self.name,
_t_na=self.teacher_name,
_cre=self.credit)
return _str
但是,当我尝试使用session.add()
添加数据时,我遇到了以下错误:
InvalidRequestError:一个或多个映射器无法初始化 - 无法继续初始化其他映射器。触发映射器:'映射器|学生|学生'。最初的例外是:初始化mapper Mapper | Student | student,expression' course'找不到姓名("名称'当然'未定义")。如果这是一个类名,请考虑在定义了两个依赖类之后将此关系()添加到类中。
答案 0 :(得分:0)
该特定错误是由输入错误和您在评论之前创建的单独Base
类引起的。我假设模型定义最初是在单独的文件中。
relationship()
需要映射的类或解析为第一个参数的字符串,但是您传递了它'course'
,它解析为该类映射到的Table
。在您的示例中,您似乎已更正了对'Course'
的引用。
命名传递给relationship()
的类的字符串参数在mapper compilation time期间通过从与Base
关联的类注册表中查找来解析。但是在您的示例中,您为每个模型和关联Base
创建了单独的Table
类,因此查找失败,因为这些类都位于不同的注册表中。
第一次使用Session
时会引发错误,因为mappers是懒惰配置的。另外,configure_mappers()
可用于显式配置任何挂起的更改。
您的Course
定义中存在另一个错误:而不是back_populates='courses'
您已使用backref='courses'
,因此在配置过程中会出现以下错误:< / p>
Traceback (most recent call last):
File "wtf.py", line 60, in <module>
configure_mappers()
File "~/Work/sqlalchemy/lib/sqlalchemy/orm/mapper.py", line 3029, in configure_mappers
mapper._post_configure_properties()
File "~/Work/sqlalchemy/lib/sqlalchemy/orm/mapper.py", line 1828, in _post_configure_properties
prop.init()
File "~/Work/sqlalchemy/lib/sqlalchemy/orm/interfaces.py", line 184, in init
self.do_init()
File "~/Work/sqlalchemy/lib/sqlalchemy/orm/relationships.py", line 1659, in do_init
self._generate_backref()
File "~/Work/sqlalchemy/lib/sqlalchemy/orm/relationships.py", line 1849, in _generate_backref
(backref_key, self, m))
sqlalchemy.exc.ArgumentError: Error creating backref 'courses' on relationship 'Course.students': property of that name exists on mapper 'Mapper|Student|student'