我在界定<代码>User和-Purchase
在Alchemy中的班级之间的关系时遇到了一个错误。 两个班级都有一个名称:purchases
。 这被用作关系背书。 然而,我已经为解决冲突做出了改变,但错误依然存在。
class Role(db.Model):
__tablename__ = roles
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(64), unique=True)
is_default = db.Column(db.Boolean, default=False, index=True)
permissions = db.Column(db.Integer)
users = db.relationship( User , backref= role , lazy= dynamic )
@staticmethod
def insert_roles():
roles = {
User : (Permission.FOLLOW |
Permission.COMMENT |
Permission.WRITE_ARTICLES, True),
Moderator : (Permission.FOLLOW |
Permission.COMMENT |
Permission.WRITE_ARTICLES |
Permission.MODERATE_COMMENTS, False),
Administrator : (0xff, False)
}
for r in roles:
role = Role.query.filter_by(name=r).first()
if role is None:
role = Role(name=r)
role.permissions = roles[r][0]
role.is_default = roles[r][1]
db.session.add(role)
db.session.commit()
def __repr__(self):
return <Role %r> % self.name
class User(UserMixin, db.Model):
__tablename__ = users
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(64), unique=True, index=True)
username = db.Column(db.String(64), unique=True, index=True)
role_id = db.Column(db.Integer, db.ForeignKey( roles.id ))
password_hash = db.Column(db.String(128))
confirmed = db.Column(db.Boolean, default=False)
name = db.Column(db.String(64))
purchases = db.relationship( Purchase , backref= user_purchases )
def __repr__(self):
return <User %r> % self.username
class Shoe(db.Model):
__tablename__ = shoes
id = db.Column(db.Integer, primary_key=True)
brand = db.Column(db.String(100))
name = db.Column(db.String(100), nullable=False, unique=True)
description = db.Column(db.String(1024))
material = db.Column(db.String(100))
price = db.Column(db.Integer, nullable=False)
color = db.Column(db.String(50))
size = db.Column(db.String(10), nullable=False)
image = db.Column(db.String(255), nullable=False, unique=True)
category_id = db.Column(db.Integer, db.ForeignKey( categories.id ))
category = db.relationship( Category , backref= shoes )
purchases = db.relationship( Purchase , back_populates= shoe )
def __repr__(self):
return <Shoe %r> % self.name
class Category(db.Model):
__tablename__ = categories
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100), nullable=False)
description = db.Column(db.String(1024))
gender = db.Column(db.String(10), nullable=False, default= U )
# Definición de las opciones de género como una lista de tuplas
GENDER_CHOICES = [
( M , Masculino ),
( F , Femenino ),
( U , Unisex ),
( N , Niño ),
( G , Niña ),
]
def __repr__(self):
return f Category {self.name}
class Purchase(db.Model):
__tablename__ = purchases
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey( users.id ))
shoe_id = db.Column(db.Integer, db.ForeignKey( shoes.id ))
user_purchases = db.relationship( User , backref= purchases )
shoe = db.relationship( Shoe , backref= purchases )
def __repr__(self):
return <Purchase %r> % self.id
sqlalchemy.exc.ArgumentError: Error creating backref user on relationship User.purchases : property of that name exists on mapper Mapper|Purchase|purchases