On Dec 2, 2010, at 11:13 AM, Alvaro Reinoso wrote:

> I think I see the error. Those are the whole tables:
> 
> class User(rdb.Model):
>       """Represents the user"""
>       rdb.metadata(metadata)
>       rdb.tablename("users")
> 
>       id = Column("id", Integer, primary_key=True)
>       name = Column("name", String(50))
>       email = Column("email", String(50))
>       password = Column("password", String(50))
>       hashed = Column("hashed", Boolean)
>       military = Column("military", Boolean)
>       agreedLicense = Column("agreed_license", Boolean)
>       userGroupId = Column("user_group_id", Integer,
> ForeignKey("user_groups.id"))
> 
>       userGroup = relationship("UserGroup", uselist=False)
>       channels = relationship("Channel", secondary=user_channels,
> order_by="Channel.titleView", backref="users")
>       mediaGroups = relationship("MediaGroup", secondary=user_media_groups,
> order_by="MediaGroup.title", backref="users")
>       screens = relationship("Screen", secondary=user_screens,
> backref="users")
>       screenGroups = relationship("ScreenGroup",
> secondary=user_screen_groups, order_by="ScreenGroup.title",
> backref="users")
> 
> class UserGroup(rdb.Model):
>       """Represents a group of users with the same features"""
>       rdb.metadata(metadata)
>       rdb.tablename("user_groups")
> 
>       id = Column("id", Integer, primary_key=True)
>       title = Column("title", String(50))
> 
>       users = relationship("User", order_by="User.name", cascade="all,
> delete", backref="user_groups")
>       permissions = relationship("Permission",
> secondary=user_group_permissions, backref="user_groups")
> 
> I have pretty similar tables and relations, but the different with
> other tables is I have this relation: userGroupId =
> Column("user_group_id", Integer, ForeignKey("user_groups.id")). So it
> seems every time when a user is created, it creates a new row in the
> user_groups and that row is related to that user. I think it's because
> of that relation.
> 
> How could I avoid this?

there's really not enough detail here to see, but the only way associating two 
existing objects together creates a row is if "secondary" is used, here if 
"user_groups" is mapped as a secondary somewhere, which I do not see.  if 
you've mapped to a table explicitly, you should not use that table as the 
"secondary" argument elsewhere, or the relationship with "secondary" should be 
set up as viewonly=True.



> 
> Thanks!
> 
> On Dec 1, 6:35 pm, Michael Bayer <mike...@zzzcomputing.com> wrote:
>> On Dec 1, 2010, at 5:46 PM, Alvaro Reinoso wrote:
>> 
>> 
>> 
>>> Hello,
>> 
>>> I have a system to manage users in my application, but I'm getting
>>> some troubles with it.
>> 
>>> Every user has to belong to a group of users. One user can only be in
>>> one group.
>> 
>>> I have those tables (inheriting from rdb.Model is basically the same
>>> thing than using sqlalchemy's declarative model)
>> 
>>> class User(rdb.Model):
>>>    """Represents the user"""
>>>    rdb.metadata(metadata)
>>>    rdb.tablename("users")
>> 
>>>    id = Column("id", Integer, primary_key=True)
>>>    name = Column("name", String(50))
>>>        ....
>> 
>>>    userGroup = relationship("UserGroup", uselist=False)
>>>        .....
>> 
>>> class UserGroup(rdb.Model):
>>>    """Represents a group of users with the same features"""
>>>    rdb.metadata(metadata)
>>>    rdb.tablename("user_groups")
>> 
>>>    id = Column("id", Integer, primary_key=True)
>>>    title = Column("title", String(50))
>> 
>>>    users = relationship("User", order_by="User.name", cascade="all,
>>> delete, delete-orphan", backref="user_groups")
>>>        ....
>> 
>>> I have a script which migrate users from a pre-existing Zope DB
>>> (object-oriented):
>> 
>>> def migrateUsers():
>>>    """Migrate all the users to the database"""
>>>    session = rdb.Session()
>>>    rScreens = session.query(Screen).all()
>>>    rUserGroups = session.query(UserGroup).all()
>>>        .....
>> 
>>>    for oldUser in grok.getSite()['Users'].values():
>>>            user = User()
>>>            ......
>>>            for newGroup in rUserGroups:
>>>                    if newGroup.title == "superadmins":
>>>                            newGroup.users.append(user)
>>>                ......
>> 
>>>    return
>> 
>>> When I execute the script, the user_groups are properly created and
>>> the users are properly added to the user_groups they should belong to,
>>> but I get empty "group" entries in the database, and I don't know why
>> 
>>> I have made some tests, and I've realized that I get an empty entry
>>> (an empty user_group) every time I try to add a user to a user_group,
>>> but I don't know what is causing this behavior.
>> 
>> theres no instantiation of UserGroup indicated above so no indication of 
>> what would be creating extra "group" rows in your database.
>> 
>> 
>> 
>>> Any hint will be appreciated.
>> 
>>> --
>>> You received this message because you are subscribed to the Google Groups 
>>> "sqlalchemy" group.
>>> To post to this group, send email to sqlalch...@googlegroups.com.
>>> To unsubscribe from this group, send email to 
>>> sqlalchemy+unsubscr...@googlegroups.com.
>>> For more options, visit this group 
>>> athttp://groups.google.com/group/sqlalchemy?hl=en.
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To post to this group, send email to sqlalch...@googlegroups.com.
> To unsubscribe from this group, send email to 
> sqlalchemy+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/sqlalchemy?hl=en.
> 

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to