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.

Make view.columns() functionality optional when creating a view

See original GitHub issue

Environment

Knex version: 0.95.14 Database + version: PostgreSQL 13.4 OS: Windows10

Feature discussion / request

As a PostgreSQL user, I would like to be able to create such queries for views: create view "users_view" as select "users".*, "user_roles"."role_code" from "users" inner join "user_roles" on "users"."user_id" = "user_roles"."user_id"

I could achieve it this with this code:

knex.schema.createView('users_view', (view) => {
    view.as(knex('users').join('user_roles', 'users.user_id', 'user_roles.user_id').select('users.*', 'user_roles.role_code'))
  })

But currently this doesn’t work because I didn’t use view.columns() functionality, so knex throws columns is not iterable error.

I edited the knex\lib\schema\viewcompiler.js file, changed part of createQuery code like this and everything worked as I wanted:

    const formatColumnsArr = [];
    for (const c of columns || []) {
      formatColumnsArr.push(
        columnize_(c, this.viewBuilder, this.client, this.bindingsHolder)
      );
    }
    const formatColumns = formatColumnsArr.length ? ' (' + formatColumnsArr.join(', ') + ')' : ''
    let sql = createStatement + this.viewName() + formatColumns;

I would be happy to provide a PR if this is something you would like.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
kibertoadcommented, Jan 16, 2022

Released in 1.0.1.

1reaction
kibertoadcommented, Nov 29, 2021

I’ll publish 1.0.0-next1 tonight then.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation: 15: CREATE VIEW
CREATE VIEW defines a view of a query. The view is not physically materialized. Instead, the query is run every time the view...
Read more >
CREATE VIEW (Transact-SQL) - SQL Server
Creates a virtual table whose contents (columns and rows) are defined by a query. Use this statement to create a view of the...
Read more >
CREATE VIEW
By default, view columns are VISIBLE regardless of their visibility in the base tables, unless you specify INVISIBLE . This applies to conventional...
Read more >
CREATE VIEW
Creates a new view in the current/specified schema, based on a query of one or more existing tables ... You can specify an...
Read more >
sql server - SQL View Select Optional
Each group can only have 2 code and always have a default code vs not a default code. · IF default code is...
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