pombreda / formalchemy

Automatically exported from code.google.com/p/formalchemy
MIT License
0 stars 0 forks source link

Elixir Compatibility: How do I get the id of the record I just created? #122

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
There should be an easy way to get the instance you just synchronized out of 
your form.  
Perhaps fieldset.sync() should return it.

Why?  Because I'm using Elixir.  Elixir adds new records to your sqlalchemy 
session automatically 
when they are instantiated.  This causes problems when I try to follow the 
example here: 
http://docs.formalchemy.org/current/pylons_sample.html#using-forms-in-controller
s .  
Specifically, it causes this:

raise Exception('Mapped instances to be bound must either have a primary key 
set or not be in a 
Session. When creating a new object, bind the class instead [i.e., bind(User), 
not bind(User())]')

This makes perfect sense, since in the example I had to instantiate the record 
first, which 
automatically added it to the session.  If instead I don't instantiate an empty 
record first, it 
properly creates the record, but I have no way of knowing its ID, so I can't 
redirect to it in the 
end.

Btw, I haven't tested it, but I'd assume a similar problem would occur if I 
tried to update an 
existing record, and this one wouldn't be so easily solvable.  See, the act of 
fetching the record to 
be updated and then changing its fields would automatically add it to the 
session before I tell 
FormAlchemy to bind it.  This should cause the same exception as above.

There isn't an easy solution to this, is there?  Just something to think about.

Original issue reported on code.google.com by nickreta...@gmail.com on 16 Oct 2009 at 6:41

GoogleCodeExporter commented 9 years ago
You can always use fs.model
Is it the trick ?

Original comment by gael.pas...@gmail.com on 21 Oct 2009 at 8:41

GoogleCodeExporter commented 9 years ago
My next question would be, how do I update a model without instantiating it?  
Or, how can I remove it from the 
sqlalchemy session to make FormAlchemy happy.

Original comment by nickreta...@gmail.com on 21 Oct 2009 at 6:33