【问题标题】:Turbogears2 AdminController throwing an error with a many-to-many relationshipTurbogears2 AdminController 抛出具有多对多关系的错误
【发布时间】:2014-11-23 05:02:46
【问题描述】:

当我尝试编辑 User、ShoppingItem 或 ShoppingList 项目(代码如下)时,我遇到了 turbogears 管理控制器引发错误的问题。出现的错误是AttributeError: 'function' object has no attribute 'primary_key'。框架中的局部变量总是返回相同的:

mapper  
<Mapper at 0x3719810; ShoppingList>
fields  
['id']
self    
<sprox.sa.provider.SAORMProvider instance at 0x03E537B0>
value   
<bound method OrderedProperties.items of <sqlalchemy.util._collections.OrderedProperties object at 0x037199F0>>
entity  
<class 'insertmealhere.model.shoppinglist.ShoppingList'>
field_name  
'items'

我无法弄清楚这与在代码中其他位置配置的其他多对多关系之间有什么不同,并且没有引发此错误。我目前在 Windows 8.1 系统上的 Python 2.7.8 上运行 Turbogears 2.2。非常感谢任何帮助。

list_item_table = Table("list_item_table", metadata,
                    Column('item_id', Integer, ForeignKey('shopping_item.id', onupdate="CASCADE", ondelete="CASCADE"), primary_key=True),
                    Column('list_id', Integer, ForeignKey('shopping_list.id', onupdate="CASCADE", ondelete='CASCADE'), primary_key=True))


class ShoppingItem(DeclarativeBase):
__tablename__ = "shopping_item"

id = Column(Integer, primary_key=True)
name = Column(String(50))
quantity = Column(String(5))
measure = Column(String(10))

# less important optional parameters that will be useful for users
brand = Column(String(50))

list_id = Column(Integer, ForeignKey('shopping_list.id'))
shopping_list = relation("ShoppingList", secondary=list_item_table, backref="items")

def get_owner_id(self):
    return self.list.user_id

@classmethod
def delete_list(cls, id, user_id):
    item = DBSession.query(cls).filter_by(id=id).one()  # get the item from the given ID
    if item.get_owner_id() == user_id:  # owned by current user
        DBSession.delete(item)  # delete from shopping list
        return True
    flash(_("You do not have authorization to perform that action."))
    return False



class ShoppingList(DeclarativeBase):
__tablename__ = 'shopping_list'

id = Column(Integer, primary_key=True)
date = Column(Date, index=True, nullable=False)
static = Column(Boolean, nullable=False, default=False)
# static is true if the items from the meal plan have been imported into the shopping list. Once done you can edit
# the items in the shopping list, remove items, etc. Until the shopping list is made static it is impossible to edit
# the items that are imported from the schedule as they do not exist in the shopping list! (and we do not want to
# edit them in the recipe!

user_id = Column(Integer, ForeignKey('tg_user.user_id'))
user = relation("User", backref="shopping_lists")

date_user_list = Index('date_user_list', 'date', 'user_id')

【问题讨论】:

    标签: python-2.7 sqlalchemy many-to-many turbogears2 sprox


    【解决方案1】:

    也许是 ShoppingItem 模型类中的 list_id = Column(Integer, ForeignKey('shopping_list.id')) 让 SQLAlchemy 感到困惑?

    【讨论】:

      猜你喜欢
      • 2021-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-05
      相关资源
      最近更新 更多