I am trying to do what I see in this question:
UPDATE in PostgreSQL generates same UUID
I have a car table with a STRING column named uuid. uuid is unique, so each row needs a different value set.
I want to set a new uuid on all car rows, and so I tried this:
UPDATE table_name
SET uuid = uuid_generate_v4();
However, when I run that I get this ERROR:
[42883] ERROR: function uuid_generate_v4() does not exist Hint: No function matches the given name and argument types. You might need to add explicit type casts.
So, I thought the uuid_generate_v4() function was something available within Postgres but I am obviously wrong.
How can I fix the error? How can I declare I function I can call in the update call so that a new unique uuid is generate and set in each row into uuid?
Related
So Im trying to make an upsert in Go for Postgres. I dynamically make my query and it comes to:
INSERT INTO v2.products (id,company_id,name)
VALUES (1,2,'foo5')
ON CONFLICT (id) DO
UPDATE
SET id = EXCLUDED.id,
company_id = EXCLUDED.company_id,
name = EXCLUDED.name
When I call db.Query with this I get an error saying:
pq: column reference "id" is ambiguous
I have tried adding the table name to the id in the ON CONFLICT ("v2.products.id") but it tells me the column doesnt exist.
When I run the same query (with just id in the ON CONFLICT braces) in Postgres it works fine
I need to add new column on my table would be uuid data type, here my code:
ALTER TABLE core.example add COLUMN newcolumn SET DATA TYPE UUID USING (uuid_generate_v4())
but show me this error:
ERROR: type modifier is not allowed for type "uuid"
LINE 1: ALTER TABLE core.example add COLUMN newsi UUID (uuid_genera...
I dont want to alter a column, would be to create a new column on my table. Any idea how to make this?
Regards
When adding a new column you don't use SET DATA TYPE. Your statement should look like:
ALTER TABLE core.example ADD COLUMN newcolumn UUID DEFAULT (uuid_generate_v4());
The DEFAULT clause will immediately fill the column with UUIDs.
Alternatively if you you just want to fill the column with initial data, you can drop the DEFAULT clause afterward:
ALTER TABLE core.example ALTER COLUMN newcolumn DROP DEFAULT;
Note that if you are using Postgres 13 and newer it is generally preferrable to use gen_random_uuid() since that method is built-in and does not rely on the uuid-ossp extension.
How can we create tsvector update trigger on Non-character data type.
For example, i have created a ts vector trigger something like this.
CREATE TRIGGER tr_doc_id_col
BEFORE INSERT OR UPDATE
ON document
FOR EACH ROW
EXECUTE PROCEDURE tsvector_update_trigger('tsv_doc_id', 'pg_catalog.english', doc_id');
Here,
tr_doc_id_col -- Name of the trigger
document -- Name of the table
tsv_doc_id -- tsvector form of the doc_id
doc_id -- Name of the column. It's data type is integer
This trigger should update the tsv_doc_id, when there is insert, delete or update happens on doc_id column.
But, the trigger is throwing an error saying that doc_id is not of character type (i.e it is not able to update based on non-character type column).
I have tried creating same kind of triggers on columns like title, body which are text data type. In this case it is working very well, but in the earlier case.
Can any of you tell me how to do in the case of non-character data type like doc_id?
tsvector_update_trigger is just a convenience function, and it does what it does. You can always write your own implementation of it with whatever embellishments you want (in this case, casting to text), but you can't change the behavior of the existing one. But it is also rather obsolete. The modern way to do it is with GENERATED columns.
create table document (
doc_id int,
tsv_doc_id tsvector generated always as (to_tsvector('english',doc_id::text)) stored
);
Although I would question the utility of a tsvector which always contains exactly one integer.
I have a column which is stored as TEXT, I would like to change it to UUID in postgres. But I need to run some function on the column first to change it to UUID. Is it possible to do smth like that?
ALTER TABLE am.product ALTER COLUMN p_merchant_id TYPE UUID USING myFunc;
Where myFunc returns UUID with the input merchant_id?
Yes, that's possible.
You just need to pass the old value to your function:
ALTER TABLE am.product
ALTER COLUMN p_merchant_id TYPE UUID USING myfunc(p_merchant_id);
What attribute of column I should use in order to get index value from postgresql sequence? valueNumeric? valueComputed?
As far as I understand the value of attribute should be nextval( 'simple_id_seq' ).
In postgresql sequence values are created as INTEGER or BIGINT.
Often this was done by using SERIAL or BIGSERIAL as column type ... but will indirectly create a sequencer of int or bigint and set the default value of the column to nextval(sequencer).
In a resultset of table data the column contains int or bigint.
Normaly there is no need to use nextval(sequencer) ... it fills the column on INSERT automatically (in the INSERT statemant the column shoult not appear).
Refer to http://www.postgresql.org/docs/9.3/static/datatype-numeric.html
If you do not want to use SERIAL or BIGSERIAL as suggested by #double_word_distruptor, use valueComputed.
With valueComputed you are telling Liquibase you are passing a function like nextval('simple_id_seq') and it will not try to parse it as a number or do any quoting.
You may also be able to use valueSequenceNext="simple_id_seq" to gain a little cross-database compatibility.