<div dir="ltr">Hi Vadim,<br><br>When you create License object it is not necessary to specify exact value of customer id. Since the `customer` attribute is defined as <span style="font-size:12.8000001907349px">Required(Customers) all that is necessary is to pass the customer object when creating a new License instance. So you can just write:<br></span><span style="font-size:12.8000001907349px"><br></span><span style="font-size:12.8000001907349px"> cust = Customer(name='asd',email='</span><a href="mailto:asd@qwe.com" target="_blank" style="font-size:12.8000001907349px">asd@qwe.com</a><span style="font-size:12.8000001907349px">')<br></span><span style="font-size:12.8000001907349px"> License(customer=cust, x=1, y=2)<br><br>The value of `customer` attribute is not customer id, but the customer object. It does not matter that this object has no assigned id value yet. During commit Pony will see that it is necessary to insert a new row for this customer object, obtain new customer id and then use that id during inserting of new license object.<br><br>Instead of the line<br><br> </span><span style="font-size:12.8000001907349px">License(customer=cust, x=1, y=2)<br></span><br><span style="font-size:12.8000001907349px">you can also write</span><br><br><span style="font-size:12.8000001907349px"> cust.licenses.create(x=1, y=2)</span><br><br><span style="font-size:12.8000001907349px">This is just the syntactic sugar.</span><br><br><span style="font-size:12.8000001907349px">In most cases, Pony can determine by itself the right order in which the new rows should be inserted to the database. The only problematic situation is when two objects have the cross-references with type of "to-one" to each other. For example, we can have Person class with the `spouse` attribute. This is the example of symmetric attribute, which links to itself:</span><br><br><span style="font-size:12.8000001907349px"> class Person(db.Entity):</span><br><span style="font-size:12.8000001907349px"> name = Required(str)</span><br><span style="font-size:12.8000001907349px"> spouse = Optional(Person, reverse='spouse')</span><br><br><span style="font-size:12.8000001907349px">Let's say we want to create two new objects `husband` and `wife` and store them to the database. We cannot just do the following:</span><br><br><span style="font-size:12.8000001907349px"> husband = Person(name='John')</span><br><span style="font-size:12.8000001907349px"> wife = Person(name='Mary')</span><br><span style="font-size:12.8000001907349px"> husband.spouse = wife</span><br><br><span style="font-size:12.8000001907349px">In this situation Pony cannot insert husband to the database, because in order to do this Pony needs to know wife id. But the same situation is with wife - Pony cannot store it to the database without storing husband first. In this case you will get an exception UnresolvableCyclicDependency. You can solve this problem by saving objects explicitly before you set up the link between them. The next code should works fine:<br></span><br><span style="font-size:12.8000001907349px"> husband = Person(name='John')</span><br><span style="font-size:12.8000001907349px"> wife = Person(name='Mary')<br></span> flush()<br><span style="font-size:12.8000001907349px"> husband.spouse = wife</span><br> <br>The `flush()` function forces Pony to store all changed or created objects to the database. At this moment all newly inserted objects will receive ids from the database. Then Pony will do update of these objects in order to establish the link between them.<br><br>So, in order to get id of the Customer object you can call `flush()` method. But in your case this is not necessary, because you have no cyclic relations between Customer and License. Just specify Customer as an object and not as id, and Pony will figure out the rest.<br><br><br>Regards,<br>Alexander<br><br></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Mar 27, 2015 at 2:32 PM, Вадим Бажов <span dir="ltr"><<a href="mailto:vadim@ideco.ru" target="_blank">vadim@ideco.ru</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div text="#000000" bgcolor="#FFFFFF">
It doesn't work for me.<br>
<br>
I have a Customers entity with 'licences' field as Set which is in
relation with Licences entity which have back relation field
'customer' as Required(Customers).<br>
<br>
Hence i must supply customer's id for 'customer' field while add
licence to a customer object.<br>
<br>
I must do the following:<br>
<br>
cust = Customer(name='asd',email='<a href="mailto:asd@qwe.com" target="_blank">asd@qwe.com</a>') <- it doesn't
create id before commiting it.<br>
<br>
lic = cust.licences.add(<br>
customer=<a href="http://cust.id" target="_blank">cust.id</a>, <- required filed in DB model. related to
Customers record by PK.<br>
x=1,<br>
y=2<br>
)<br>
<br>
But newly created object has no id since it wasn't written to DB
yet.<br>
<br>
May be Pony allows child entities without Required fields linking
entity back to a parent entity, but i didn't try it.<span class=""><br>
<br>
<div>On 25.03.2015 11:03, Matthew Bell
wrote:<br>
</div>
</span><div><div class="h5"><blockquote type="cite">
<div dir="ltr">
<div>
<div>
<div>You don't need the PK to add a relation, you can do:<br>
<br>
<font face="FreeMono">cust =
Customers(name='GazMyas',city='Chelyabinsk')</font> <br>
</div>
cust.licenses.add(License(x=1, y=2))<br>
cust.licenses.add(License(x=3, y=4))<br>
<br>
</div>
So, create the customer, then loop over all licenses for that
customer, doing customer.licenses.add<br>
<br>
</div>
You can then commit after each iteration, or just once at the
end of the script, up to you.<br>
</div>
<div class="gmail_extra"><br>
<div class="gmail_quote">On 25 March 2015 at 08:28, Вадим Бажов
<span dir="ltr"><<a href="mailto:vadim@ideco.ru" target="_blank">vadim@ideco.ru</a>></span>
wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div text="#000000" bgcolor="#FFFFFF"> Hello happy pony
users and devs !<br>
<br>
I do an import from old db to a new one, driven by pony
orm. Import iterates over records of customers and their
related licences, fetched from an old DB. So we have to
fill two tables in new DB: Customers table and Licence
table with records, which have one2many relations (one
Customer can have many Licences, by PK). PK is an
autoincrementing integer in both tables.<br>
<br>
Within <b>each iteration</b> of import i need to write
one Customer record and its Licences records to new DB.
Licence records should have customer_id fields filled up
with corresponding Customer PKs. Here we have a problem:<br>
<br>
If we create an object of an entity Customers : <font face="FreeMono">cust =
Customers(name='GazMyas',city='Chelyabinsk')</font> , it
wont be recorded to a DB exactly at this moment and we
wont able to use its ID (pk of the record) to bind Licence
records to it. In other words there is no '<a href="http://cust.id" target="_blank">cust.id</a>' element at this moment.<br>
<br>
We can do a 'commit()' each time we create a Customer
object. After that '<a href="http://cust.id" target="_blank">cust.id</a>' comes
ready. But in case of monstrous imports with over 20 000
records committing every record to hdd slows down the
import process to hours. And hangs up your server hard
drive. Anyway, it's a bad practice to commit every record
walking the huge array of incoming data.<br>
<br>
So, by now, I fill up two dicts with Customers records,
incrementing their ID's manually (customer_id += 1) and
Licences records bound to this pre-calculated ID's. When
dicts are ready, I write them to a new DB walking through
them.<br>
<br>
Is there a better way to complete a data import without
filling up transition dicts and calculating autoincrement
IDs by hand ? Can I somehow use PKs of the records before
committing them to DB ?<br>
</div>
<br>
_______________________________________________<br>
ponyorm-list mailing list<br>
<a href="mailto:ponyorm-list@ponyorm.org" target="_blank">ponyorm-list@ponyorm.org</a><br>
<a href="/ponyorm-list" target="_blank">/ponyorm-list</a><br>
<br>
</blockquote>
</div>
<br>
<br clear="all">
<br>
-- <br>
<div>Regards,<br>
<br>
Matthew Bell<br>
</div>
</div>
<br>
<fieldset></fieldset>
<br>
<pre>_______________________________________________
ponyorm-list mailing list
<a href="mailto:ponyorm-list@ponyorm.org" target="_blank">ponyorm-list@ponyorm.org</a>
<a href="/ponyorm-list" target="_blank">/ponyorm-list</a>
</pre>
</blockquote>
<br>
</div></div></div>
<br>_______________________________________________<br>
ponyorm-list mailing list<br>
<a href="mailto:ponyorm-list@ponyorm.org">ponyorm-list@ponyorm.org</a><br>
<a href="/ponyorm-list" target="_blank">/ponyorm-list</a><br>
<br></blockquote></div><br></div>