Re: [sqlalchemy] How make update in Association Objects

2016-03-19 Thread Christopher Lee
In your TypeUser class you have a relationship user, which has a backref of
'type'.  That should allow you to say:

t = Type.query...
user.type = t
session.merge(user)
session.commit()

On Wed, Mar 16, 2016 at 9:08 AM, Mário Idival  wrote:

> Hi,
>
> I have a problema to make updates on tables with Association Objects
>
>
> class Type(PrimaryKey, Base):
> __tablename__ = "types"
>
> name = Column(Unicode, nullable=False)
> description = Column(Unicode)
>
> users = relationship(User, secondary="type_users")
>
> class TypeUser(Base):
> __tablename__ = "type_users"
>
> type_id = Column(Integer, ForeignKey("types.pk"), primary_key=True)
> user_id = Column(Integer, ForeignKey("users.pk"), primary_key=True)
>
> type = relationship(Type, backref=backref("type_users",
> lazy="dynamic"))
> user = relationship(User, backref=backref("type", uselist=False))
>
> class User(PrimaryKey, Base):
> __tablename__ = "users"
> # some irrelevant datas.
>
> To save is easy...
>
> user = User.query.get(pk)
> type = Type.query.get(other_pk)
>
> type.users.append(user)
>
>
>
> How I can update o `type` of user??
>
> --
> You received this message because you are subscribed to the Google Groups
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sqlalchemy+unsubscr...@googlegroups.com.
> To post to this group, send email to sqlalchemy@googlegroups.com.
> Visit this group at https://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


[sqlalchemy] How make update in Association Objects

2016-03-18 Thread Mário Idival
Hi,

I have a problema to make updates on tables with Association Objects


class Type(PrimaryKey, Base):
__tablename__ = "types"

name = Column(Unicode, nullable=False)
description = Column(Unicode)

users = relationship(User, secondary="type_users")

class TypeUser(Base):
__tablename__ = "type_users"

type_id = Column(Integer, ForeignKey("types.pk"), primary_key=True)
user_id = Column(Integer, ForeignKey("users.pk"), primary_key=True)

type = relationship(Type, backref=backref("type_users", lazy="dynamic"))
user = relationship(User, backref=backref("type", uselist=False))

class User(PrimaryKey, Base):
__tablename__ = "users"
# some irrelevant datas.

To save is easy...

user = User.query.get(pk)
type = Type.query.get(other_pk)

type.users.append(user)



How I can update o `type` of user??

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.