2016-04-14 126 views
1

我使用SQLAlchemy的ORM來獲取屬於某一用戶,但我得到以下SQLAlchemy的多對多的關係參數

錯誤無法確定加入的父/子表之間的條件對 關係AppRole的角色。用戶 - 有多個外鍵路徑 通過輔助表'app_user_role'鏈接表。指定 'foreign_keys'參數,提供 應計爲的列的列表,其中包含從 輔助表到每個父表和子表的外鍵引用。

該表的定義是:

class AppUserRole(Base_Write): 
    __tablename__ = 'app_user_role' 
    id = Column(Integer, primary_key=True) 
    created_at = Column(DateTime, server_default=text('NOW()'), nullable=False) 
    created_by = Column(Integer, ForeignKey('app_user.id'), nullable=False) 
    is_deleted = Column(Boolean, server_default='0', nullable=False) 
    date_deleted = Column(DateTime) 
    deleted_by = Column(Integer, ForeignKey('app_user.id')) 
    user_id = Column(Integer, ForeignKey('app_user.id'), nullable=False) 
    role_id = Column(Integer, ForeignKey('app_role.id'), nullable=False) 
    date_assigned = Column(DateTime, nullable=False) 


class AppRole(Base_Write): 
    __tablename__ = 'app_role' 
    id = Column(Integer, primary_key=True) 
    created_at = Column(DateTime, server_default=text('NOW()'), nullable=False) 
    created_by = Column(Integer) 
    is_deleted = Column(Boolean, server_default='0', nullable=False) 
    date_deleted = Column(DateTime) 
    deleted_by = Column(Integer, ForeignKey('app_user.id')) 
    role_classification_id = Column(Integer, ForeignKey('app_role_classification.id'), nullable=False) 
    role_code = Column(String(10), nullable=False, unique=True) 
    role_name = Column(String(30), nullable=False) 

    users = relationship("AppUser", secondary="app_user_role", back_populates="app_role") 


class AppUser(Base_Write): 
    __tablename__ = 'app_user' 
    id = Column(Integer, primary_key=True) 
    created_at = Column(DateTime, server_default=text('NOW()'), nullable=False) 
    created_by = Column(Integer, ForeignKey('app_user.id'), nullable=False) 
    is_deleted = Column(Boolean, server_default='0', nullable=False) 
    date_deleted = Column(DateTime) 
    deleted_by = Column(Integer, ForeignKey('app_user.id')) 
    username = Column(String(50), nullable=False, unique=True) 

    roles = relationship("AppRole", secondary="app_user_role", back_populates="app_user") 

查詢正在運行的是:

for app_user in session.query(AppUser).\ 
filter(AppUser.is_deleted == False).filter(AppUser.id == user_id): 
    #print the names of the roles assigned 
    print(app_user.roles.role_name) 

的事情是,我已經在那裏我已經定義的其他多對多的關係其他表參數和一切工作。雖然我不斷收到這個錯誤。在這裏還有其他類似的問題,但嘗試了不同的建議,但沒有運氣。我嘗試去錯誤被建議和使用我的前一個問題here on SO,但即使把foreign_keys選項似乎沒有工作,儘管我必須承認我真的猜猜在哪裏放置它。

SQLAlchemy的版本== 1.0.12和PostgreSQL 9.5

回答

1

由於錯誤消息說,你有多個外鍵鏈接AppRoleAppUser,即AppUserRole.user_idAppUserRole.deleted_byAppUserRole.created_by,所以你需要指定foreign_keys參數:

class AppRole(Base): 
    ... 
    users = relationship("AppUser", secondary="app_user_role", 
         foreign_keys=[AppUserRole.user_id, AppUserRole.role_id], 
         back_populates="roles") 


class AppUser(Base): 
    ... 
    roles = relationship("AppRole", secondary="app_user_role", 
         foreign_keys=[AppUserRole.user_id, AppUserRole.role_id], 
         back_populates="users") 
+0

This Works!謝謝。然而,雖然'back_populates'通常指向定義了關係的'table/class/model',這意味着在這種情況下,我會認爲對於AppRole它將是'back_populates =「app_role」'和AppUser表它會是'back_populates =「app_user」'? – lukik

+0

@lukik不,它應該是對方*關係*的名稱,而不是表格。 – univerio

相關問題