English 中文(简体)
采购定义中的卡片卢布冲突在用户和采购类别中归属
原标题:Troubleshooting a SQLAlchemy conflict in the definition of the purchases attribute in User and Purchase classes

我在界定<代码>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 
问题回答

暂无回答




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签