On 01/05/2016 10:29 PM, Jim Shepherd wrote:
> I am trying to create a class (Model) that ensures that when an instance
> is created, a specific set of children objects (Parameters) exist based
> on a separate relationship (Option) and if not, delete or add objects
> (Parameters) as necessary.  
> 
> Here is sample code of a very simplified version of what I am trying to
> accomplish:
> 
> |
> fromsqlalchemy importColumn,ForeignKey,create_engine,inspect
> fromsqlalchemy.orm importrelationship,sessionmaker
> fromsqlalchemy.ext.declarative importdeclarative_base
> fromsqlalchemy.types importInteger,String
> 
> Base=declarative_base()
> engine =create_engine('sqlite://')
> 
> Session=sessionmaker(bind=engine)
> session =Session()
> 
> classOption(Base):
>     __tablename__ ="option"
>     key =Column("OptionId",Integer,primary_key=True)
>     optionType =Column("OptionType",String)
> 
> classParameter(Base):
>     __tablename__ ="parameter"
>     key =Column("ParameterId",Integer,primary_key=True)
>     modelId =Column("ModelId",Integer,ForeignKey("model.ModelId"))
>     model =relationship("Model")
> 
> classModel(Base):
>     __tablename__ ="model"
>     key =Column("ModelId",Integer,primary_key=True)
>     optionId =Column("OptionId",Integer,ForeignKey(Option.key))
> 
>     option =relationship("Option")
>     parameters =relationship("Parameter")
> 
>     def__init__(self,**keywords):
>         Base.__init__(self,**keywords)
>         print("transient",inspect(self).transient)
>         print("option in init:",self.option)
>         session.add(self)
>         print("pending",inspect(self).pending)
>         print("option after add:",self.option)
>         session.flush()
>         print("persistent",inspect(self).persistent)
>         print("option after flush:",self.option,self.option.optionType)
>         ifself.option.optionType =="normal"andnotself.parameters:
>             print("adding parameter")
>             self.parameters.append(Parameter(model=self))
>         print("parameters:",self.parameters)
>              
> 
> 
> metadata =Base.metadata
> metadata.create_all(engine)
> 
> 
> option =Option(key=1,optionType="normal")
> session.add(option)
> model =Model(key=1,optionId=1)
> 
> |
> 
> 
> My questions:
> 
>  1. I like the checks and object modification within __init__ for
>     consistency, but are there reasons not to do so?

the part with the conditional and the
"self.parameters.append(Parameter(...)" aspect is exactly the best way
to do that kind of thing - when you want the object to have particular
state when created, __init__ is where that happens most naturally.

The session.add(self) part, not so much because your class is hardwired
to that one specific session.  Also the flush() on every __init__ would
potentially cause more python/DB traffic on an object-by-object basis
that might work out better as one big flush() when needed.

>  2. As in this example, there are some cases that the Model instances
>     are created using the foreign key id of the option instead of an
>     instance of Option. In this case, the model instance is transient
>     during __init__ so the self.options relationship is None until the
>     model instance is flushed (as in the example). Are there issues with
>     flushing during __init__?

the flush() is not terrible but you might want to pass in the Session
itself as an argument so that the class isn't hardcoded to that one
session and/or scoped session.  Also, instead of the flush you perhaps
want to just session.query(Option).get(option_id) for that option_id
that's passed in, since you want to look at other state that's on it.
if that Option is already loaded in memory then the get() won't emit any
further SQL.



>  3. Is there a better way to accomplish this need?
> 
> 
> Thanks!
> 
> -- 
> 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
> <mailto:sqlalchemy+unsubscr...@googlegroups.com>.
> To post to this group, send email to sqlalchemy@googlegroups.com
> <mailto: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.

Reply via email to