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

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);

Related

how to Insert SQL query from previous database table value to pass to another database table with dynamic value in Katalon

Hi any one can help me on this issue which i have problem insert a dynamic value which is from previous database table value to pass to another table in Katalon.
Please find my information below:-
This screenshot is ab.dbo.DOCUMENT table which DOCUMENT_ID is auto populate with value
which mean it will appear random number by itself.
Another screenshot is bc.dbo.DOCUMENT_IC table which i need to manually key in DOCUMENT_ID in
the value base on on what it is given from ab.dbo.DOCUMENT table DOCUMENT_ID.
Attached of a screenshot for bc.dbo.DOCUMENT_IC
In Katalon i am using a keyword to connect my database, insert query and close
connection. I am aware of this step and able to connect to database with katalon. But i
am not very sure how to pass a dynamic value from ab.dbo.DOCUMENT table DOCUMENT_ID which
it can randomly appear a number value to bc.dbo.DOCUMENT_IC table DOCUMENT_ID which i need to
manually key in a value base on the value given.
Below is my Katalon script:-
Hopefully someone can help me on this
Thank you.
If I have a table with an auto incrementing ID in one table and I need that value elsewhere I would typically write sql like this :
insert into firsttable (Document_Type) values ('PDF');
insert into secondtable (Document_ID, App_ref_Num) values (##Identity, 'somenumber')
In the databases I have worked with ##Identity will give you the integer or id of the last inserted row. If you can't run multiple statements most connection libraries will have something like a $conn->insert_id that will do the same thing as running select ##identity.

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

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.

Creating a record history table - How do I create a record on creation?

For a project, I want to have a "History" table for my records. I have two tables for this (example) system:
RECORDS
ID
NAME
CREATE_DATE
RECORDS_HISTORY
ID
RECORDS_ID
LOG_DATE
LOG_TYPE
MESSAGE
When I insert a record into RECORDS, how can I automatically create an associated entry in RECORDS_HISTORY where RECORDS_ID is equal to the newly inserted ID in RECORDS?
I currently have a sequence on the ID in RECORDS to automatically increment when a new row is inserted, but I am unsure how to prepopulate a record in RECORDS_HISTORY that will look like this for each newly created (not updated) record.
INSERT INTO RECORDS_HISTORY (RECORDS_ID, LOG_DATE, LOG_TYPE, MESSAGE) VALUES (<records.id>, sysdate(), 'CREATED', 'Record created')
How can I create this associated _HISTORY record on creation?
You didn't mention the DB you are working with. I assume its Oracle. The most obvious answer is: Use a "On Insert Trigger". You even can get back the ID (sequence) from the insert statement into table RECORDS. Disadvantages of this solution: Triggers are kinda "hidden" code, can slow down processes on massive inserts and you consume like double diskspace on storing data partially redundant. What if RECORDS got updated or deleted? Can that happen and do you have to take care of that as well? The big question is: What is your goal?
There are proved historisation concepts around. Have a look at this: https://en.wikipedia.org/wiki/Slowly_changing_dimension

How to pass parameters while triggering a package or else how to find the last modified record in a table?

Whenever a row is modified or inserted newly in the database ( Oracle 10g), there is a trigger on one column considering it is of particular value, it triggers a package. I have a stored procedure inside that package which will need this record which triggered these. There is no column that is storing timestamps. How can we do this ? I'm sure there will be a work around. Any help is appreciated,if atleast a guidance in the right direction.
Assuming that your table has a primary key ID, you can find the record by :new.id.

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