Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for View Creation #1626

Closed
thefatoneinthecorner opened this issue Aug 13, 2016 · 10 comments · Fixed by #4748
Closed

Support for View Creation #1626

thefatoneinthecorner opened this issue Aug 13, 2016 · 10 comments · Fixed by #4748

Comments

@thefatoneinthecorner
Copy link

Hi,

Is there any support for view creation (analogous to createTableIfNotExists and friends)?

Many thanks,

-- Alastair

@KMayne
Copy link

KMayne commented Aug 9, 2017

+1 This would be very useful since Knex acts as an abstraction over different SQL syntaxes between our development (SQLite) and production (Postgres) databases.

@elhigu
Copy link
Member

elhigu commented Aug 15, 2017

Sorry, no such support in knex so far. @KMayne you just need to create own query abstraction with knex.raw depending if it is ran on sqlite or pg.

@mashaalmemon
Copy link
Contributor

@elhigu I was looking for the ability to have view creation/maintainence in migrations as well.

You mention that knex.raw can be used. Can you give a quick example of how to achieve this using knex.raw? I'm assuming you don't mean create the entire view with knex.raw, but just sub in 'create view' vs 'create table' somehow with knex.

We use postgres in our project.

@elhigu
Copy link
Member

elhigu commented Dec 18, 2018

What kind of query you need to have generated?

@mashaalmemon
Copy link
Contributor

Something like these (depending on the options we're looking to use at view creation time):

CREATE VIEW SELECT a.*, b.* WHERE a.b_id=b.id;
CREATE VIEW SELECT a.*, b.* WHERE a.b_id=b.id WITH LOCAL CHECK OPTION;
CREATE VIEW SELECT a.*, b.* WHERE a.b_id=b.id WITH CASCADED CHECK OPTION;

@elhigu
Copy link
Member

elhigu commented Dec 19, 2018

This should work for your cases:

knex.schema.raw(`CREATE VIEW ? WITH CASCADED CHECK OPTION`, [
  knex.select('a.*', 'b.*').whereColumn('a.b_id','b.id')
]) 

@gpetrov
Copy link
Contributor

gpetrov commented Jan 10, 2021

Any news on views creation in the Knex API? Would be really nice to have an generic API integration instead of database dependent creation commands.

@kibertoad
Copy link
Collaborator

@gpetrov This functionality is still missing, but any contributions on that front would be dearly appreciated.

@gpetrov
Copy link
Contributor

gpetrov commented Jan 10, 2021

Maybe @rijkvanzanten can help - there is also a great need for query the views schema, next to creating and managing them. All databases, even SQLite have Views support and those are pretty powerful.

@kibertoad
Copy link
Collaborator

This is now available in 0.95.12-rc4

OlivierCavadenti added a commit to AbeonaPascha/knex that referenced this issue Nov 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants