Есть две таблицы - Студенты и Специальности
Суть в том, что у студента есть поля - Приоритет1, Приоритет, Приоритет3 и они должны быть связаны со специальностями по id
например. А у меня SQLAlchemy ругается на множественное несоответствие ключей. Всю голову сломал с этими parent/child relationships
.
Подскажите пожалуйста решение проблемы.
Привожу код:
class Student(db.Model):
id = db.Column(db.Integer, primary_key = True, nullable = False)
UID = db.Column(db.String(64), unique = True, nullable = False)
Last_Name = db.Column(db.String(64), nullable = False)
First_Name = db.Column(db.String(64), nullable = False)
Middle_Name = db.Column(db.String(64), nullable = False)
Avg_mark = db.Column(db.Float, default = '0', nullable = False)
Math_mark = db.Column(db.Float, default = '0', nullable = False)
Courses_mark = db.Column(db.Float, default = '0', nullable = False)
Privils = db.Column(db.Boolean, nullable = False)
Priority_1 = db.Column(db.Integer, db.ForeignKey('specialty.id'), nullable = False)
Priority_2 = db.Column(db.Integer, db.ForeignKey('specialty.id'), nullable = False)
Priority_3 = db.Column(db.Integer, db.ForeignKey('specialty.id'), nullable = False)
def __repr__(self):
return '<Student %r>' % (self.Last_Name)
class Specialty(db.Model):
id = db.Column(db.Integer, db.ForeignKey('specialty.id'), primary_key = True, nullable = False)
Title = db.Column(db.String(64), nullable = False)
Abbr = db.Column(db.String(64), nullable = False)
Budg_Places = db.Column(db.Integer, nullable = False)
prior1 = db.relationship('Student', backref = 'prior1', foreign_keys="specialty.id")
prior2 = db.relationship('Student', backref = 'prior2', foreign_keys="specialty.id")
prior3 = db.relationship('Student', backref = 'prior3', foreign_keys="specialty.id")
def __repr__(self):
return '<Speciality %r>' % (self.Title)
SQLAlchemy: 2.6.1
До этого пока ковырял были другие ошибки.
Текущие ошибки:
models.Student.query.all()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/prvz/Desktop/WORK/reception-dtpa/flask/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 498, in __get__
mapper = orm.class_mapper(type)
File "/Users/prvz/Desktop/WORK/reception-dtpa/flask/lib/python2.7/site-packages/sqlalchemy/orm/base.py", line 421, in class_mapper
mapper = _inspect_mapped_class(class_, configure=configure)
File "/Users/prvz/Desktop/WORK/reception-dtpa/flask/lib/python2.7/site-packages/sqlalchemy/orm/base.py", line 400, in _inspect_mapped_class
mapper._configure_all()
File "/Users/prvz/Desktop/WORK/reception-dtpa/flask/lib/python2.7/site-packages/sqlalchemy/orm/mapper.py", line 1167, in _configure_all
configure_mappers()
File "/Users/prvz/Desktop/WORK/reception-dtpa/flask/lib/python2.7/site-packages/sqlalchemy/orm/mapper.py", line 2768, in configure_mappers
mapper._post_configure_properties()
File "/Users/prvz/Desktop/WORK/reception-dtpa/flask/lib/python2.7/site-packages/sqlalchemy/orm/mapper.py", line 1710, in _post_configure_properties
prop.init()
File "/Users/prvz/Desktop/WORK/reception-dtpa/flask/lib/python2.7/site-packages/sqlalchemy/orm/interfaces.py", line 183, in init
self.do_init()
File "/Users/prvz/Desktop/WORK/reception-dtpa/flask/lib/python2.7/site-packages/sqlalchemy/orm/relationships.py", line 1628, in do_init
self._process_dependent_arguments()
File "/Users/prvz/Desktop/WORK/reception-dtpa/flask/lib/python2.7/site-packages/sqlalchemy/orm/relationships.py", line 1653, in _process_dependent_arguments
setattr(self, attr, attr_value())
File "/Users/prvz/Desktop/WORK/reception-dtpa/flask/lib/python2.7/site-packages/sqlalchemy/ext/declarative/clsregistry.py", line 281, in __call__
x = eval(self.arg, globals(), self._dict)
File "<string>", line 1, in <module>
AttributeError: 'Table' object has no attribute 'id'