I am using Sequel gem to update a dataset.
If you do an update on the model instance, the updated_at timestamp gets updated just fine:
foo = Foo[id: 1]
foo.update(blah: blah)
However, when you call a batch update on the dataset, it does not update the updated_at timestamps for every entry.
Foo.where(id: 1).update(blah: blah)
I looked at the Sequel docs regarding the timestamps plugin, which I am using in my code already, but the updated_at fields will still not be updated using the batch update. Can someone help me with this issue?
Running Foo.where(id: 1).update(blah: blah) executes a single query on the database, which is not going to run model instance-level hooks. You can either update each record individually:
Foo.where(id: 1).all{|f| f.update(blah: blah)}
or you can switch to a database-trigger based approach, such as https://github.com/jeremyevans/sequel_postgresql_triggers
There's an update_all function you could look at. It's an active record method so it should trigger the updated_at.
Update: okay i just checked and it does not trigger, you should use his answer!
Related
Can anyone help me to automatically save the history of all changed columns in a model to a history column in the same table as JSON format with column(s) name, original value, changed to, changed by?
I am looking for Traits like centralized function to use in all models.
I am using Laravel 8 with PostgreSQL.
This answer is maybe outdated since it is Laravel 4. But if it works, it is quickest way to insert log in DB wherever save() method is called for that model.
https://stackoverflow.com/a/20694395/13893004
I'm fetching a specific record with a DB table using
$myTableObj = MyTable::where(['type' => $sometype])->first();
Getting it successfully, updating some fields and saving with
$myTableObj->save();
Surprisingly, this record is updated along with another record that also has 'type' = $sometype. What can be done to prevent this?
NOTE: originally the table did not have the auto increment id field, but I have read in forums that it may make problems in Laravel so I did add it, which did not solve the problem.
Method save() working with 'id' filed only.
You can try this
$myTableObj = MyTable::where(['type' => $sometype])->update(['something' => 'value']);
Source
I understand update() is to update, but, my answer works fine and fits good for update too. Its useful where you dont want columns to be defined once again for update, (sp when they are not fillable, its tested with primary key as condition)
$myTableObj->save(); basically its for saving new record, if you want to update that row you can update like below code:
$myTableObj=new MyTable;
$myTableObj->exists=true;
$myTableObj->type=$sometype;//this is your condition, identify
$myTableObj->update();
I think what's happening here is Laravel is saving as well as updating row.
I'm using Fluent NHibernate, and am attempting to update all the records for entity type "Files". I have eager loaded FileTypes along with it. By practice all tables have an UpdatedTime even if they are not necessary for use.
The problem I'm having is the following classic:
SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM
This is coming from the FileType's "UpdatedTime = null" even though my update method is being called on a File.
I do not want to overwrite the null in FileType, in fact I don't want to update it at all.
It seems to me that i should be able to either elect to not include the related entities in the update via mapping, or I should be able to get it to leave that field as null instead of DateTime.min.
EDIT
Below I have provided a solution to the the datetime issue, however, it would be a superior answer to prevent the second table from updating in the first place.
The answer turned out being relatively simple after more messing around. I changed the domain to use DateTime? instead of DateTime.
While this solved the problem, It is a bit 'hacky' and I would prefer to not update the second table.
I'm trying to find some nice work arounds for the issues of computed columns in code first. Specifically, I have a number of CreatedAt datetime columns that need to be set to getdate().
I've looked at doing this via the POCO constructors, but to do that I must remove the Computed option (or it won't persist the data), however, there is no easy to way ensure the column is only set if we are inserting a record. So this would overwrite the CreatedAt each time we update.
I'm looking to create an alter script that can be called after the DropCreate that would go through and alter various columns to include the default value of getdate().
Is there an event to hook into something like OnDropCreateCompleted where I could then run additional SQL
What would be the best way handle the alter script? I am thinking just sending raw sql to the server that would run.
Is there another way to handle the getdate() issue that might be more graceful and more inline with code first that I'm missing?
Thanks
You can just make custom initializer derived from your desired one and override Seed method where you can execute any SQL you want to use - here is some example for creating such initializer.
If you are using migrations you can just the custom SQL to Up method.
I have a memory based session table containing the following:
id
sessnhash
userid
created_at
updated_at
The idea is that when a user first logs in a random sessnhash is created and passed back so that all further actions have to send that sessionhash for server requests. These go through a loggedin? method on the application controller which simply checks that a row in the table exists for that sessionhash.
I now want to extend this functionality to keep a tab of the last time any activity happened for that user and I thought one of way of doing this would be to immediately do a save after finding the sessionhash, hoping that this would then update the 'updated_at' attribute.
However Rails is too clever and doesn't update anything as in reality nothing has been updated.
Is there any way of forcing Rails to update the updated_at attribute without having to make any data changes to any of the other attributes?
I've just learnt that I need to do.
sessn.touch
which will update the updated_at attribute.
It's all in here: touch.