title: How to create a child SQL-Alchemy instance from a parent instance?
body: <p>I have two models, <code>Signal</code> and <code>Trade</code>. My <code>Trade</code> object inherits its attributes from <code>Signal</code>, as almost all of the attributes are the same, however there are some key functions and attributes which make it different. I'm writing a method on <code>Signal</code> called <code>as_trade()</code>, I want to create a <code>Trade</code> instance from all the data/attributes on the <code>Signal</code> instance so I can save it to the database. I was using deepcopy - and this had been working, but in actual fact I don't think this is the correct/best way to do it. Is there a tried/tested way of performing an action like this?</p>
  
  <pre><code>class Signal(db.Model):
      __tablename__ = 'signal'
      id = db.Column(db.Integer, primary_key=True)
      class_type = db.Column(db.String())
  
      __mapper_args__ = {
          'polymorphic_identity': 'signal',
          'polymorphic_on': class_type,
      }
  
      # MAIN FIELDS
      type = db.Column(db.String(20))
  
      def as_trade(self):
         signal_dict = self.__dict__
         for key in ['id', 'user_id', '_sa_instance_state', 'class_type']:
             signal_dict.pop(key)
         trade = Trade(**signal_dict)
         return trade
  
      ...
  
  class Trade(Signal):
      __tablename__ = 'trade'
      id = db.Column(db.Integer, db.ForeignKey('signal.id'), primary_key=True)
  
      __mapper_args__ = {
          'polymorphic_identity': 'trade',
      }
      ....
  
  </code></pre>
  
  <p>I should add, the issue I'm getting here is that when <code>as_trade()</code> is called, it throws a key error. If I call <code>self.id</code> in PDB, I get a value (1). However, if I call <code>self.__dict__</code> I get only an InstanceState, with no attributes. Hence, it throws a keyError. But sometimes, by calling self_dict and self.<strong>dict</strong> whilst in PDB, I can get the dictionary to actually appear and have values - this is the part I don't understand. Left alone, the program fails here as it throws a keyError given that self.<strong>dict</strong> gives only <code>InstanceState</code> object. But poking around in PDB and calling self.<strong>dict</strong> a step ahead in the process allows the for loop to complete because the dictionary appears with all the attributes required...?</p>
  
  <pre><code>{'_sa_instance_state': &lt;sqlalchemy.orm.state.InstanceState object at 0x000002E694983B88&gt;
  }
  
  </code></pre>
  
  <p>UPDATE:
  The reason its acting funky is because by popping <code>_sa_instance_state</code> from the dictionary, the state is acting up. If I leave <code>_sa_instance_state</code> in the dictionary object and pass it to Trade(), I get an error <code>TypeError: '_sa_instance_state' is an invalid keyword argument for Trade</code></p>