I acquired an easy condition in SQLAlchemy. I've one model inside a table, allows refer to it as Model1 here. I wish to give a row within this table, and obtain the autoincremented key, in order to create another model by using it, and employ this key. This isn't a problematic database design (1:1 relation etc). I merely need this type in another table, since the other table has been moved to some remote host, and that i require the matching secrets therefore the servers will understand one another. There won't be any further local reference between these 2 tables, also it's difficult to produce relations due to that.

Think about the following code:

object1 = model.Model1(param)
DBSession.add(object1)

# if I do this, the line below fails with an UnboundExecutionError.
# and if I dont do this, object1.id won't be set yet
#transaction.commit()

object2 = model.AnotherModel(object1.id) #id holds the primary, autoincremented key

I wanted I would not even need to commit "by hand". Essentially what I must achieve is, "Model1" is continually growing, with growing Model.id primary key. AnotherModel is definitely merely a little fraction of Model1, which has not been processed yet. Obviously I possibly could give a flag in "Model1", a boolean area within the table to mark already processed elements, however i was wishing this wouldn't be necessary.

How do i get my above code working?

Greets,

Tom

A few things:

  • Would you please explain exactly what the variable transaction is certain to?
  • Precisely what statement boosts the UnboundExecutionError?
  • Please supply the full exception message, including stack trace.
  • The 'normal' factor to complete within this situation, is always to call DBSession.flush(). Perhaps you have attempted that?

Example:

object1 = Model1(param)
DBSession.add(object1)
DBSession.flush()
assert object1.id != None # flushing the session populates the id

object2 = AnotherModel(object1.id)

For any great explanation towards the SA session and what flush() does, see Using the Session.

Essentially, flush() causes Pending instances being Persistent - and therefore new objects are Placed in to the database tables. flush() also UPDATEs the tables with values for instances the session tracks which has changes.

commit() always issues flush() first.

Inside a transaction, you are able to flush multiple occasions. Each flush() causes UPDATEs and/or Card inserts within the database. The whole transaction could be commited or folded back.

I have only used this with ForeignKeys, which means you within the second situation rather would do model.AnotherModel(model1=object1), after which it simply labored (tm). And So I suspect this might be an issue with your models, so perhaps you can publish them as well?

if you wish to get new primary key identifiers to create without anything being committed, just call session.flush(). Which will emit everything pending towards the database inside the current transaction.