question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to create a column with a function as its default value?

See original GitHub issue

Hi,

id: { type: 'uuid', primaryKey: true, defaultValue: 'uuid_generate_v4()' },

That column spec fails with

[WARN] Using unknown data type UUID
[ERROR] error: invalid input syntax for uuid: "uuid_generate_v4()"
    at Connection.parseE (C:\Users\critc\AppData\Roaming\npm\node_modules\db-migrate-pg\node_modules\pg\lib\connection.js:539:11)
    at Connection.parseMessage (C:\Users\critc\AppData\Roaming\npm\node_modules\db-migrate-pg\node_modules\pg\lib\connection.js:366:17)
    at Socket.<anonymous> (C:\Users\critc\AppData\Roaming\npm\node_modules\db-migrate-pg\node_modules\pg\lib\connection.js:105:22)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:188:7)
    at readableAddChunk (_stream_readable.js:172:18)
    at Socket.Readable.push (_stream_readable.js:130:10)
    at TCP.onread (net.js:542:20)

Is there some special notation to stop it being treated as a literal?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

7reactions
Haharincommented, Aug 23, 2019

For anyone else looking the correct way to do this is:

exports.up = (pgm) => { 
  pgm.createExtension('uuid-ossp'); // add uuid support to pg
  pgm.createTable('things', {
    id: { type: 'uuid', primaryKey: true, default: pgm.func('uuid_generate_v4()') },
  });
};

pgm.createExtension is not a function

can you help with this?

2reactions
webjaycommented, May 10, 2021

Here’s what works in May of 2021:

const tableName = 'my_log';

exports.up = async function(db) {
  await db.runSql('CREATE EXTENSION IF NOT EXISTS "pgcrypto";');
  return db.createTable(tableName, {
    id: { type: 'uuid', primaryKey: true, defaultValue: new String('gen_random_uuid()') },
    payload: { type: 'text' },
    created_at: { type: 'timestamp', notNull: true, defaultValue: new String('now()') },
  });
};

You’ll see [WARN] Using unknown data type UUID but it works.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Add Column to Pandas DataFrame with a Default Value
The three ways to add a column to Pandas DataFrame with Default Value. Using pandas.DataFrame.assign(**kwargs); Using [] operator ...
Read more >
Add new column to Pandas dataframe with default value
A step-by-step Python code example that shows how to add new column to Pandas DataFrame with default value. Provided by Data Interview Questions, ......
Read more >
How to set a function as a default value in SQL Server?
What I want to do is to show first three characters of my first column's data which is Product_Name . For example my...
Read more >
How to add a column with a default value to an existing table ...
To SQL add a column with a default value is a simple operation in SQL. Let us set up a 'student' table as...
Read more >
Specify default column values | BigQuery - Google Cloud
Functions are evaluated when the data is written to the table. The type of the default value must match or coerce to the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found