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.

DefaultTo postgres functions

See original GitHub issue

Is there a way to set a defaultTo to a column a postgresql function? For example If I want to have an uuid column auto generate using uuid_generate_v4().

I’m currently running this as a separate migration setting the default using raw sql. Is there any way to do it as part of the migration itself?

For example

t.uuid('transact_id').defaultTo(default.uuid_generate_v4());

Issue Analytics

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

github_iconTop GitHub Comments

9reactions
rhys-vdwcommented, Jun 27, 2016

Have you tried:

t.uuid('transact_id').defaultTo(knex.raw('uuid_generate_v4()');
4reactions
paulbjensencommented, Jan 29, 2018

Note to add, for Postgres 9 and greater, you will need to run this SQL on the database to enable using uuid_generate_v4() functions:

CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

This resolves an issue where running a migration on a PostgreSQL DB errors on a message like “function uuid_generate_v4() does not exist”.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation: 15: 5.2. Default Values
In a table definition, default values are listed after the column data type. For example: CREATE TABLE products ( product_no integer, name text,...
Read more >
values in function, arguments dependency, PostgreSQL
CREATE OR REPLACE FUNCTION years_compare( IN year1 integer DEFAULT NULL, IN year2 integer DEFAULT NULL ) year1 = COALESCE(year1, ...
Read more >
Default value for column postgres function with argument
Unfortunately, you cannot do that, because of (for the documentation):. The value is any variable-free expression (subqueries and ...
Read more >
How to set a column default to a PostgreSQL function using ...
Postgresql – How to set a column default to a PostgreSQL function using SQLAlchethe. postgresqlsqlalchemyuuid. All of my tables defined in SQLAlchemy have ......
Read more >
PostgreSQL Functions
Most PostgreSQL functions accept connection as the optional first parameter. ... Apache (perhaps other servers, too) running the server as (default to) ...
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