node.js - Add Column with random value Sequelize PostgreSQL -
i want add column not_null
constraint column contain random default values following code how can this
up: function (queryinterface, sequelize, done) { queryinterface.addcolumn( { tablename: 'sometable', schema: 'sometemplate' }, 'somecolumn', //column name { //column date type , constraint type: sequelize.string, allownull: false, defaultvalue: // want random value }) .then(function () { done() }) .catch(done); }
postgresql example:
create or replace function f_random_text( length integer ) returns text $body$ chars ( select unnest(string_to_array('a b c d e f g h j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9', ' ')) _char ), charlist ( select _char chars order random() limit $1 ) select string_agg(_char, '') charlist ; $body$ language sql; drop table if exists tmp_test; create temporary table tmp_test ( id serial, data text default f_random_text(12) ); insert tmp_test values (default, default), (default, default) ; select * tmp_test; id | data ----+-------------- 1 | rymujh4e0niq 2 | 7u4029bokaej (2 rows)
apparently can this. (of course, can add other characters well, or use other random string generator - this, example.) ref: https://dba.stackexchange.com/questions/19632/how-to-create-column-in-db-with-default-value-random-string
Comments
Post a Comment