简体   繁体   English

子表中许多值的空用户 ID

[英]Empty user id for many value in child table

I have a problem in sqlalchemy.我在 sqlalchemy 中遇到问题。 I have one to many relationship:我有一对多的关系:

class User(Base):
    __tablename__ = 'users'

    id = Column(Integer, primary_key=True)
    name = Column(String(255))
    tags = relationship('Tag', back_populates='users')

class Tag(Base):
    __tablename__ = 'tags'

    id = Column(Integer, primary_key=True)
    tag_name = Column(String(50))
    user_id = Column(Integer, ForeignKey('users.id'))
    users = relationship('User', back_populates='tags')

When i save tags in this way当我以这种方式保存标签时

user = session.query(User).filter(User.id == user_id).first()
user.tags = [Tag(tag_name=tag_name)]
session.add(user)
session.commit()

All tags saved but when o opened my db i see that only for last tag have value of user id and for other tags this filed empty.所有标签都保存了,但是当我打开我的数据库时,我看到只有最后一个标签具有用户 ID 值,而其他标签则为空。 在此处输入图像描述 But test must have user_id =1.但测试必须有 user_id =1。 How can i fixed this problem?我该如何解决这个问题? For framework i use sanic and db is posgresql.对于框架,我使用 sanic,db 是 posgresql。 I want to save for user with id =1 many tags like: [user_id=1, tag='tad1'], [user_id=1, tag='tad2'] and [user_id=1, tag='tad3'].我想为 id =1 的用户保存许多标签,例如:[user_id=1, tag='tad1'], [user_id=1, tag='tad2'] 和 [user_id=1, tag='tad3']。 As a result i have may users but each users have own multiple tags.结果我有可能的用户,但每个用户都有自己的多个标签。

It is difficult to tell, because you are not showing the full context in which you are adding multiple tags, but if you are using something like this:很难说,因为您没有显示添加多个标签的完整上下文,但是如果您使用的是这样的东西:

def add_tag(user_id,tag_name):
  user = session.query(User).filter(User.id == user_id).first()
  user.tags = [Tag(tag_name=tag_name)]
  session.add(user)
  session.commit()

ANd then executing it like this:然后像这样执行它:

for tag in tags:
  add_tag(user_id=user.id,tag_name=tag.name)

Then because you use user.tags = [Tag(tag_name=tag_name)], you would be overwriting all previous tags rather than appending to them.然后因为您使用 user.tags = [Tag(tag_name=tag_name)],您将覆盖所有以前的标签而不是附加到它们。 This would explain why only the last tag appears to be connected to the user.这可以解释为什么只有最后一个标签似乎与用户相关联。 To append new tags rather than overwriting you should use append() like so:对于 append 新标签而不是覆盖你应该像这样使用 append() :

def add_tag(user_id,tag_name):
  user = session.query(User).filter(User.id == user_id).first()
  user.tags.append(Tag(tag_name=tag_name))
  session.add(user)
  session.commit()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flask-SQLAlchemy:如何使用用户 ID 从多对多辅助表中删除行 - Flask-SQLAlchemy: How to delete row from many-to-many secondary table with the user id django"<user: user556> " 在使用这种多对多关系之前,"id" 字段需要有一个值</user:> - django "<User: user556>" needs to have a value for field "id" before this many-to-many relationship can be used 查询表的多对多字段值 - Query with many to many field value of a table 烧瓶通过ID进行多对多循环,无法将数据插入表中 - Flask Many to Many loop through id to insert data to table not working 查询表中具有不同值的ID - Query table for id of distinct value SQLAlchemy一对多,没有子表具有主键 - SQLAlchemy one-to-many without the child table having a primary key 检查用户是否从多个字段中选择了一个值 - Check if user has chosen a value from many to many field 在reportlab中,将尽可能多的空行添加到最后一页的长表中 - In reportlab add as many empty rows to a long table as fit on the last page 通过表 BooleanField 对多对多中单个 True 值的约束 - Constraint of a single True value in many-to-many through table BooleanField Django,多对多关系,如何为表中的所有键插入值? - Django, many to many relationship, how to insert value for all keys in table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM