What's the difference between Gorm Save and Update? - go-gorm

What are the differences between these two ways of updating? Aside from the fact that the 1st way requires a query first. The docs say that updating the second way will ignore zero value fields. Is that also true of the 1st?
user = User{1, "old name"}
user2 = User{2, "old name"}
//fist way
db.First(&user)
user.Name = "new name"
db.Save(&user)
//second way
user2.Name = "new name"
db.Model(&user2).Updates(&user2)

As per Gorm API documentation (https://pkg.go.dev/gorm.io/gorm#DB.Save), Save function will create a Primary Key for an entity if it does not exist as well as update fields, while Update will just update fields.

As per gorm's docs, Updates differs from Save in 2 ways:
It does not create an entity if it does not exist as mentioned before.
It only updates non-zero values so you can specify specify only the properties you need to update.

Related

Why related fields use Write function

_name = "my.table"
building_id = fields.Many2one('building', related='floor_id.building_id', readonly=False)
floor_id = fields.Many2one('building.floor')
A user with the read access to 'building' and 'building.floor' tables, tries to create a record in "my.table" If the user chooses building_id and floor_id together an error occurs. The error says that my user has no access to write 'building.floor' table. My question is: why a related field use the write function, what is the difference between the compute and related in this scenario?
Related fields are very simple computed fields. So simple they can be "implemented" with one parameter on field definition. Odoo has generic methods for those fields. For example a lot of developers don't write inverse methods for computed fields, which inverse the compute method, because the simply don't need it. But without it and without storing the computed field, Odoo sets the field readonly.
Related fields have a generic inverse method. In your case changing building_id when there was already a floor_id chosen, Odoo will write the building_id on that floor_id.building_id, because that's how related fields work (i know that's not the best explanation).
The user obviously has no write/update rights on builiding.floor model and that's why there will be the access error message in the end, because Odoo wants to write the new building on the floor.
Seems to me you want to filter the floors by buildings, but you shouldn't use a related field for that. Just put a domain on floor_id which filters by the chosen building_id:
floor_id = fields.Many2one('building.floor', domain="[('building_id', '=?', building_id)]")
You could also use domain operator =, but =? will show all floors when no building was set yet.

Co dependent fields in Odoo 9.0 view

Hello everybody and thanks for taking the time to read my question:
I need to make two fields update on each other changes in my view, let's say one of them list's ingredients and the other lists food requirements
requirements = fields.Selection([(0,"Vegan"),(1,"Lactose intolerant")])
ingredients_id = fields.Many2Many(comodel_name="sample.ingredients")
What i need is to update ingredients_id's domain to show only those who match a requirement and vice versa, if you happend to select and ingredient the other field selecting the proper requirement. I'm thinking about making field A a computed field depending on fieldB and then add an on_change to field B that updates field A. But I'm pretty certain that will turn into some kind of infinite loop situation. Is that my only option or am I missing something obviuos?
Use #api.depends decorator and pass both fields as parameters in the decorator. This will trigger function every time on change on both values and will be dependent on each other.
#api.depends('requirements','ingredients_id')
def function (self): ....
do something....
return

How to implement a key lookup for generated keys table in pentaho Kettle

I just started to use Pentaho Kettle for integration. Seems great so far, quite intuitive compared to Talend, which I was also investigating.
I am trying to migrate some customers without their keys. So I have their email addresses.
The customer may already exist in the database, so what I need to do is:
If the customer exists, add it's id to the imported field and continue.
But if the customer doesn't exist I need to get the next Hibernate key from the table Hibernate_Sequences and set it as the id.
But I don't want to always allocate a key, so I want to conditionally execute a step to allocate the next key.
So what I want to do, is in the flow execute the db procedure, which allocates the next key and returns it, only if there's no value in id from the "lookup id" step.
Is this possible?
Just posting my updated flow - so the answer was to use a filter rows component which splits the data on true/false. I really had trouble getting the id out of the database stored proc because of a bug, so I had to use decimal and then convert back to integer (which I also couldn't figure out how to do, so used a javascript component).
Yes it is. As per official documentation (i left only valuable information) "Lookup values are added as new fields onto the stream". So u need just to put step "Filter row" in Flow section and check for "id" which suppose to be added in "Existing Id Lookup" step.

How do I get NHibernate to save an entity if I assign it an ID, but generate one otherwise?

According to the REST philosophy, a PUT request should update the resource at a URL if it exists, and create it if it doesn't exist. So in other words, if I use the following URL:
PUT http://server/item/5
If an Item exists with an ID of 5, it will be updated. If an Item doesn't exist with an ID of 5, a new Item will be created with an ID of 5.
However, I'm using NHibernate for persistence, and I mapped my IDs as Identity. This means that no matter what value I assign the ID, NHibernate will replace it with its own when I save a new Item.
How do I get NHibernate to save an Item with the ID that I assign it, without changing the ID mapping to Assigned?
If you use Identity, the DB won't allow you to enter a value.
That said, if your DB has some special syntax to allow inserting with explicit values in Identity fields, you can implement your own generator, which I guarantee will be error prone, hard to debug, and not very useful. But it's possible.
Study everything in https://nhibernate.svn.sourceforge.net/svnroot/nhibernate/trunk/nhibernate/src/NHibernate/Id and start creating your Frankenstein :-)

Adding more promoted properties after a first one in BizTalk

I've got a BizTalk solution where I added a schema, and promoted a field
- this added the default PropertySchema.xsd with the element in
A bit later, added another schema, but when I try and promote a couple of fields, but when I select Show Promotions, I end up mapping the new field to one of the existing fields in the PropertySchema
My bad ... I should have used the Quick Promote
You can manually add items to the property schema as you would with correlation and then associate the promoted property.