Python 如何定义SQLAlchemy中的反向多态性? 桌子
家长:Python 如何定义SQLAlchemy中的反向多态性? 桌子,python,python-3.x,sqlalchemy,Python,Python 3.x,Sqlalchemy,家长: id | child_type | child_id --- | ---------- |-------- 1 | Student | 5 7 | Student | 8 9 | Employee | 3 15 | Employee | 8 29 | Employee | 12 id | name --- | ---------- 1 | Jerry .. | ... 5 | Alex 8 | Tom id | name -
id | child_type | child_id
--- | ---------- |--------
1 | Student | 5
7 | Student | 8
9 | Employee | 3
15 | Employee | 8
29 | Employee | 12
id | name
--- | ----------
1 | Jerry
.. | ...
5 | Alex
8 | Tom
id | name
--- | ----------
3 | John
.. | ...
8 | Mike
12 | Susan
from sqlalchemy.ext.declarative import declarative_base, AbstractConcreteBase
from sqlalchemy.schema import Column, ForeignKey
from sqlalchemy.sql.sqltypes import Integer, String
Base = declarative_base()
class Parent(Base):
__tablename__ = 'parents'
id = Column(Integer, pirmary_key=True)
child_type = Column(String)
child_id = Column(Integer)
child = relationship('Child', uselist=False)
class Child(AbstractConcreteBase, Base):
name = Column(String)
class Student(Child):
__tablename__ = 'students'
__mapper_args__ = {
'polymorphic_identity': 'Student',
'concrete': True
}
id = Column(Integer, ForeignKey(Parent.child_id), primary_key=True)
class Employee(Child):
__tablename__ = 'employees'
__mapper_args__ = {
'polymorphic_identity': 'Employee',
'concrete': True
}
id = Column(Integer, ForeignKey(Parent.child_id), primary_key=True)
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from models import Parent
if __name__ == '__main__':
engine = create_engine('mysql~~', encoding='utf-8')
session = sessionmaker(bind=engine)()
parent_success_1 = session.query(Parent).get(1) # <Parent(id=1, child_type='Student', child_id=5)>
assert parent_success_1.child.name == 'Alex' # success
parent_success_2 = session.query(Parent).get(15) # <Parent(id=15, child_type='Employee', child_id=8)>
assert parent_success_2.child.name == 'Mike' # success
parent_failure = session.query(Parent).get(7) # <Parent(id=7, child_type='Student', child_id=8)>
assert parent_failure.child.name == 'Tom' # failed
# sqlalchemy.orm.exc.MultipleResultsFound: Multiple rows were found for one()
学生:
id | child_type | child_id
--- | ---------- |--------
1 | Student | 5
7 | Student | 8
9 | Employee | 3
15 | Employee | 8
29 | Employee | 12
id | name
--- | ----------
1 | Jerry
.. | ...
5 | Alex
8 | Tom
id | name
--- | ----------
3 | John
.. | ...
8 | Mike
12 | Susan
from sqlalchemy.ext.declarative import declarative_base, AbstractConcreteBase
from sqlalchemy.schema import Column, ForeignKey
from sqlalchemy.sql.sqltypes import Integer, String
Base = declarative_base()
class Parent(Base):
__tablename__ = 'parents'
id = Column(Integer, pirmary_key=True)
child_type = Column(String)
child_id = Column(Integer)
child = relationship('Child', uselist=False)
class Child(AbstractConcreteBase, Base):
name = Column(String)
class Student(Child):
__tablename__ = 'students'
__mapper_args__ = {
'polymorphic_identity': 'Student',
'concrete': True
}
id = Column(Integer, ForeignKey(Parent.child_id), primary_key=True)
class Employee(Child):
__tablename__ = 'employees'
__mapper_args__ = {
'polymorphic_identity': 'Employee',
'concrete': True
}
id = Column(Integer, ForeignKey(Parent.child_id), primary_key=True)
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from models import Parent
if __name__ == '__main__':
engine = create_engine('mysql~~', encoding='utf-8')
session = sessionmaker(bind=engine)()
parent_success_1 = session.query(Parent).get(1) # <Parent(id=1, child_type='Student', child_id=5)>
assert parent_success_1.child.name == 'Alex' # success
parent_success_2 = session.query(Parent).get(15) # <Parent(id=15, child_type='Employee', child_id=8)>
assert parent_success_2.child.name == 'Mike' # success
parent_failure = session.query(Parent).get(7) # <Parent(id=7, child_type='Student', child_id=8)>
assert parent_failure.child.name == 'Tom' # failed
# sqlalchemy.orm.exc.MultipleResultsFound: Multiple rows were found for one()
员工:
id | child_type | child_id
--- | ---------- |--------
1 | Student | 5
7 | Student | 8
9 | Employee | 3
15 | Employee | 8
29 | Employee | 12
id | name
--- | ----------
1 | Jerry
.. | ...
5 | Alex
8 | Tom
id | name
--- | ----------
3 | John
.. | ...
8 | Mike
12 | Susan
from sqlalchemy.ext.declarative import declarative_base, AbstractConcreteBase
from sqlalchemy.schema import Column, ForeignKey
from sqlalchemy.sql.sqltypes import Integer, String
Base = declarative_base()
class Parent(Base):
__tablename__ = 'parents'
id = Column(Integer, pirmary_key=True)
child_type = Column(String)
child_id = Column(Integer)
child = relationship('Child', uselist=False)
class Child(AbstractConcreteBase, Base):
name = Column(String)
class Student(Child):
__tablename__ = 'students'
__mapper_args__ = {
'polymorphic_identity': 'Student',
'concrete': True
}
id = Column(Integer, ForeignKey(Parent.child_id), primary_key=True)
class Employee(Child):
__tablename__ = 'employees'
__mapper_args__ = {
'polymorphic_identity': 'Employee',
'concrete': True
}
id = Column(Integer, ForeignKey(Parent.child_id), primary_key=True)
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from models import Parent
if __name__ == '__main__':
engine = create_engine('mysql~~', encoding='utf-8')
session = sessionmaker(bind=engine)()
parent_success_1 = session.query(Parent).get(1) # <Parent(id=1, child_type='Student', child_id=5)>
assert parent_success_1.child.name == 'Alex' # success
parent_success_2 = session.query(Parent).get(15) # <Parent(id=15, child_type='Employee', child_id=8)>
assert parent_success_2.child.name == 'Mike' # success
parent_failure = session.query(Parent).get(7) # <Parent(id=7, child_type='Student', child_id=8)>
assert parent_failure.child.name == 'Tom' # failed
# sqlalchemy.orm.exc.MultipleResultsFound: Multiple rows were found for one()
文件夹 型号.py:
id | child_type | child_id
--- | ---------- |--------
1 | Student | 5
7 | Student | 8
9 | Employee | 3
15 | Employee | 8
29 | Employee | 12
id | name
--- | ----------
1 | Jerry
.. | ...
5 | Alex
8 | Tom
id | name
--- | ----------
3 | John
.. | ...
8 | Mike
12 | Susan
from sqlalchemy.ext.declarative import declarative_base, AbstractConcreteBase
from sqlalchemy.schema import Column, ForeignKey
from sqlalchemy.sql.sqltypes import Integer, String
Base = declarative_base()
class Parent(Base):
__tablename__ = 'parents'
id = Column(Integer, pirmary_key=True)
child_type = Column(String)
child_id = Column(Integer)
child = relationship('Child', uselist=False)
class Child(AbstractConcreteBase, Base):
name = Column(String)
class Student(Child):
__tablename__ = 'students'
__mapper_args__ = {
'polymorphic_identity': 'Student',
'concrete': True
}
id = Column(Integer, ForeignKey(Parent.child_id), primary_key=True)
class Employee(Child):
__tablename__ = 'employees'
__mapper_args__ = {
'polymorphic_identity': 'Employee',
'concrete': True
}
id = Column(Integer, ForeignKey(Parent.child_id), primary_key=True)
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from models import Parent
if __name__ == '__main__':
engine = create_engine('mysql~~', encoding='utf-8')
session = sessionmaker(bind=engine)()
parent_success_1 = session.query(Parent).get(1) # <Parent(id=1, child_type='Student', child_id=5)>
assert parent_success_1.child.name == 'Alex' # success
parent_success_2 = session.query(Parent).get(15) # <Parent(id=15, child_type='Employee', child_id=8)>
assert parent_success_2.child.name == 'Mike' # success
parent_failure = session.query(Parent).get(7) # <Parent(id=7, child_type='Student', child_id=8)>
assert parent_failure.child.name == 'Tom' # failed
# sqlalchemy.orm.exc.MultipleResultsFound: Multiple rows were found for one()
main.py:
id | child_type | child_id
--- | ---------- |--------
1 | Student | 5
7 | Student | 8
9 | Employee | 3
15 | Employee | 8
29 | Employee | 12
id | name
--- | ----------
1 | Jerry
.. | ...
5 | Alex
8 | Tom
id | name
--- | ----------
3 | John
.. | ...
8 | Mike
12 | Susan
from sqlalchemy.ext.declarative import declarative_base, AbstractConcreteBase
from sqlalchemy.schema import Column, ForeignKey
from sqlalchemy.sql.sqltypes import Integer, String
Base = declarative_base()
class Parent(Base):
__tablename__ = 'parents'
id = Column(Integer, pirmary_key=True)
child_type = Column(String)
child_id = Column(Integer)
child = relationship('Child', uselist=False)
class Child(AbstractConcreteBase, Base):
name = Column(String)
class Student(Child):
__tablename__ = 'students'
__mapper_args__ = {
'polymorphic_identity': 'Student',
'concrete': True
}
id = Column(Integer, ForeignKey(Parent.child_id), primary_key=True)
class Employee(Child):
__tablename__ = 'employees'
__mapper_args__ = {
'polymorphic_identity': 'Employee',
'concrete': True
}
id = Column(Integer, ForeignKey(Parent.child_id), primary_key=True)
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from models import Parent
if __name__ == '__main__':
engine = create_engine('mysql~~', encoding='utf-8')
session = sessionmaker(bind=engine)()
parent_success_1 = session.query(Parent).get(1) # <Parent(id=1, child_type='Student', child_id=5)>
assert parent_success_1.child.name == 'Alex' # success
parent_success_2 = session.query(Parent).get(15) # <Parent(id=15, child_type='Employee', child_id=8)>
assert parent_success_2.child.name == 'Mike' # success
parent_failure = session.query(Parent).get(7) # <Parent(id=7, child_type='Student', child_id=8)>
assert parent_failure.child.name == 'Tom' # failed
# sqlalchemy.orm.exc.MultipleResultsFound: Multiple rows were found for one()
parent\u失败。child
返回:
[<Student(id=8, name='Tom')>, <Employee(id=8, name='Mike')>]
[,]
如何将
polymorphic\u on
设置为父级的child\u type列以使用它?这在文档中讨论如下。我想你正在寻找的方法是。@univerio谢谢!