使用 SQLAlchemy 关系,同时避免导入的循环依赖

问题描述 投票:0回答:1

我在使用 db.relationships 时遇到问题。我注意到,要在查询中使用关系,通过关系引用另一个模型的每个模型都必须了解该模型(通过导入,如下面的示例所示)。我的所有模型都在单独的文件中,所以我担心我会面临循环依赖问题。任何人都可以建议我如何克服这个限制。

从.models.role导入角色

class User(db.Model, BaseModel):
    __tablename__ = 'user'

    id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4, unique=True, nullable=False)
    first_name = db.Column(String(255), nullable=False)
    last_name = db.Column(String(255), nullable=False)
    email = db.Column(String(255), nullable=False)
    role_id = db.Column(UUID(as_uuid=True), db.ForeignKey('role.id'), nullable=False)
    
    role = db.relationship("Role", back_populates="user")
    

from .models.user import User

class Role(db.Model):
    __tablename__ = 'role'

    id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4, unique=True, nullable=False)
    name = db.Column(String(60), nullable=False)
    description = db.Column(String(255), nullable=True)

    users = db.relationship('User', back_populates='role')
python flask-sqlalchemy relationship
1个回答
0
投票

为了避免循环依赖,您可以做的一件事是使用别名导入模块,如下所示:

# models/user.py

from .models import role as role_model

class User(db.Model, BaseModel):
    __tablename__ = 'user'

    id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4, unique=True, nullable=False)
    first_name = db.Column(String(255), nullable=False)
    last_name = db.Column(String(255), nullable=False)
    email = db.Column(String(255), nullable=False)
    role_id = db.Column(UUID(as_uuid=True), db.ForeignKey('role.id'), nullable=False)
    
    role = db.relationship("role_model.Role", back_populates="user")
# models/role.py

from .models import user as user_model

class Role(db.Model):
    __tablename__ = 'role'

    id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4, unique=True, nullable=False)
    name = db.Column(String(60), nullable=False)
    description = db.Column(String(255), nullable=True)

    users = db.relationship('user_model.User', back_populates='role')

您还可以尝试导入子模块,如下所示:

# models/user.py

from .models import role

class User(db.Model, BaseModel):
    __tablename__ = 'user'

    id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4, unique=True, nullable=False)
    first_name = db.Column(String(255), nullable=False)
    last_name = db.Column(String(255), nullable=False)
    email = db.Column(String(255), nullable=False)
    role_id = db.Column(UUID(as_uuid=True), db.ForeignKey('role.id'), nullable=False)
    
    role = db.relationship("role.Role", back_populates="user")
# models/role.py

from .models import user

class Role(db.Model):
    __tablename__ = 'role'

    id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4, unique=True, nullable=False)
    name = db.Column(String(60), nullable=False)
    description = db.Column(String(255), nullable=True)

    users = db.relationship('user.User', back_populates='role')
© www.soinside.com 2019 - 2024. All rights reserved.