Laravel automatically add "on update CURRENT_TIMESTAMP" to some certain columns - laravel

My migration is like so:
$table->bigIncrements('id');
$table->timestamp('from_date');
$table->timestamp('to_date');
$table->timestamps();
The problem is that when I migrate it, the second one which is from_date automatically gets on update CURRENT_TIMESTAMP attribute so it means when I update other columns this column will be updated too. That's what I don't want. How can I prevent it?

You can add nullable to the column in order to remove the constraint.
$table->timestamp('from_date')->nullable();
Unfortunately I think that this is the only solution. Then you can add a form validation in order to prevent setting null value for the field.
-- EDIT
$table->timestamp('from_date')->default(DB::raw('CURRENT_TIMESTAMP'));
// or
$table->timestamp('from_date')->useCurrent();
Try this as well, I believe this is what the created_at has.

You need to make the DateTime column nullable, then MySQL won't add that. By default, MySQL adds that to the first timestamp in the table, unless explicitly told not to (via allowing a null value for the field). This is a MySQL thing, not a Laravel thing.
$table->timestamp('colName')->nullable();
Read :
Automatic Initialization and Updating for TIMESTAMP and DATETIME - MYSQL DOCS
Laravel & MySQL auto-adding “on update current_timestamp()” to timestamp fields

This behaviour is native to MySQL. You can read about it on this documentation page:
TIMESTAMP and DATETIME columns have no automatic properties unless they are specified explicitly, with this exception: If the explicit_defaults_for_timestamp system variable is disabled, the first TIMESTAMP column has both DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP if neither is specified explicitly.
The docs go on to specify that there are two strategies for avoiding this behaviour:
Enable the explicit_defaults_for_timestamp variable in your database configuration.
When defining your timestamp column, specify a DEFAULT or make the column nullable.

Related

How to specify in supabase a field should only have default value?

I'm making a basic chat app using supabase.
I've figured out how to make it so users can only create/edit their profile and send messages from their profile using RLS, by checking if their id matches with the auth.id.
The problem I'm facing is, that I don't want users to be able to insert messages with wrong creation dates.
If no date is specified the default value is correct. But currently there is no check for the creation date.
Can I specify, that the creation date field has to be sent empty?
Is there maybe a better solution?
One thing you can do is you can override the created_at field with the current server timestamp using triggers.
Code not verified, so you might have to make some small adjustments, but the idea is this:
create extension if not exists moddatetime schema extensions;
-- assuming the table name is "messages", and a timestamp column "created_at"
-- this trigger will set the "created_at" column to the current timestamp for every insert
create trigger
handle_created_at before insert
on messages
for each row execute
procedure moddatetime(created_at);

How to set the CURRENT_TIMESTAMP into certain column in timestamps function?

I am using Laravel 5.4, i read the docs said that, there are two function that use really similar name, it's timestamps and timestamp:
Timestamps
$table->timestamps();
Adds nullable created_at and updated_at TIMESTAMP equivalent columns.
Timestamp
$table->timestamp('name_column');
TIMESTAMP equivalent column.
As you can see, timestamps created a default two column, created_at and updated_at with default TIMESTAMP attribute. How could i make only one of the column, which updated_at have CURRENT_TIMESTAMP attribute which just using a single command ?
I have done like below:
$table->timestamps()->useCurrent();
But i got an error instead using above command, so instead i used default(\DB::raw('CURRENT_TIMESTAMP')). To solved this problem, i have to make two command, like below:
$table->timestamp('created_at');
$table->timestamp('updated_at')->default(\DB::raw('CURRENT_TIMESTAMP'));
How could i make that happen ?

Oracle Apex Tabular Form Data Lost

I have a Tabular Form in my Oracle Apex application. There is a column as UPDATED_DATE. I have set Tabular Form Attributes of this column as below.
When i'm inserting data, UPDATED_DATE column in database table is filling with a value. But when i'm updating existing record, UPDATED_DATE column is not filling with a new value. old date is remaining without changing to a newer data.
How can i solve this ?
The default will only set a value if the field is empty. On your first entry the field is empty, and therefore the updated_Date is set. On updating, the field is allready set, and therefore won't be updated.
You need to create a dynamic action (assuming you use apex 4 or above) and use that to update the date field. Your dynamic action should be triggerd on change of an table row and subsequently set the date field to the current date.
Let me now if you need help on the dynamic action.

Adding column with default value

I have a table A (3 columns) in production which is around 10 million records. I wanted to add one more column to that table and also I want to make default value to 1. Is it going to impact production DB performance If add a column with default value 1 or something else. What would be best approach to this to avoid any kind of performance impact on DB? your thoughts are much appreciated!!
In Oracle 11g the process of adding a new column with a default value has been considerably optimized. If a newly added column is specified as NOT NULL, default value for that column is maintained in the data dictionary and it's no longer required for a default value of a column to be stored for all records in a table, so it's no longer required to update each record with a default value. Such an optimization considerably reduces amount of time the table is exclusively locked during the operation.
alter table <tab_name> add(<col_name> <data_type> default <def_val> not null)
Moreover, column with a default value added that way will not consume space, until you deliberately start to update that column or insert a record with a non default value for that column. So the operation of adding a new column with a default value and not null constraint specified completes pretty quick.
i think that it is better that you create a table as backup table with this syntax:
create table BackUpTable as SELECT * FROM YourTable;
alter table BackUpTable add (newColumn number(5,0)default 1);

Maximum number of columns in a LINQ to SQL object?

I have 62 columns in a table under SQL 2005 and LINQ to SQL doesn't handle the updates though the reading would work just fine, I tried re-adding the table to the model, created a new data model but nothing worked, I'm guessing I've hit the maximum number of columns limit on an object, can anyone explain that ?
I suspect there is some issue with an identity or timestamp column (something autogenerated on the SQL server). Make sure that any column that is autogenerated is marked that way in the model. You might also want to look at how it is handling concurrency. If you have triggers that update any values on the row after it is updated (changing values) and it is checking all columns on updates, this would cause the update to fail. Typically I create my tables with a timestamp column -- LINQ2SQL picks this up when I generate the model and uses it alone for concurrency.
Solved, either one of the following two
-I'm using a UniqueIdentifier column that was not set as Primary key
-Set Unique ID primary key, checked the properties of the same column in Server Explorer and it was still not showing as Primary key, refreshed the connection,dropped the same table on the model and voila.
So I assume I made a change to my model some time before, deleted the table from the model and added the same from the Server explorer without refreshing the connection and it never used to work.
Question is, does VS Server Explorer maintain it's own table schema and requires connection refresh everytime a change is made in the database ?
There is no limit to the number of columns LINQ to SQL will handle.
Have you got other tables updating successfully?
What else is different about how you are accessing the table content?

Resources