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:

from sqlalchemy import Column, ForeignKey, create_engine, inspect
from sqlalchemy.orm import relationship, sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.types import Integer, String

Base = declarative_base()
engine = create_engine('sqlite://')

Session = sessionmaker(bind=engine)
session = Session()

class Option(Base):
    __tablename__ = "option"
    key = Column("OptionId", Integer, primary_key=True)
    optionType = Column("OptionType", String)

class Parameter(Base):
    __tablename__ = "parameter"
    key = Column("ParameterId", Integer, primary_key=True)
    modelId = Column("ModelId", Integer, ForeignKey("model.ModelId"))
    model = relationship("Model")

class Model(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)
        if self.option.optionType == "normal" and not self.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?
   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__?
   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.
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