Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@maximelkin
Copy link
Collaborator

fixes #4620
This breaks wrong code, when onDelete/onUpdate called not in context of foreign constraints.
But also breaks valid code, i.e.:

table.integer('num').references('non_exist.id').index('num_idx').onDelete('CASCADE');

(index() method return ColumnBuilder interface, not ReferencingColumnBuilder)

This probably could be fixed with rewriting code to extending previous interface with new methods instead of current approach, idk is it worth it?

@lorefnon

@kibertoad
Copy link
Collaborator

@zeotuan Any thoughts?

@zeotuan
Copy link
Contributor

zeotuan commented Sep 4, 2021

if we change the index(indexName?:string): ColumnBuilder to index(indexName?:string): ReferrencingColumnBuilder It would temporarily solve this problem right? But i am not sure if it would be a good solution though. @maximelkin @lorefnon what do you guys think?

@maximelkin
Copy link
Collaborator Author

Now this is more close to knex js implementation, but this is kinda hack.

@maximelkin maximelkin requested a review from lorefnon September 13, 2021 03:55
@maximelkin
Copy link
Collaborator Author

@zeotuan can you check PR?

@kibertoad
Copy link
Collaborator

@zeotuan Ping?

@zeotuan
Copy link
Contributor

zeotuan commented Oct 2, 2021

I tried this code and got the same result as your @maximelkin. What is the advantage of your compared to this?

type ReferencingColumnBuilder = {
    [K in keyof ColumnBuilder]: (...args) =>  ReferencingColumnBuilder
  } & {
    inTable(tableName: string): ReferencingColumnBuilder;
    deferrable(type: deferrableType): ReferencingColumnBuilder;
    withKeyName(keyName: string): ReferencingColumnBuilder;
    onDelete(command: string): ReferencingColumnBuilder;
    onUpdate(command: string): ReferencingColumnBuilder;
  };

Also if you believe your solution is better than the one above can you make it this way so it's easier to understand

type ReferencingColumnBuilder = {
    [K in keyof ColumnBuilder]: ColumnBuilder[K] extends (...args: infer TArgs) => any 
      ? (...args: TArgs) => ReferencingColumnBuilder 
      : ColumnBuilder[K];
  } & {
    inTable(tableName: string): ReferencingColumnBuilder;
    deferrable(type: deferrableType): ReferencingColumnBuilder;
    withKeyName(keyName: string): ReferencingColumnBuilder;
    onDelete(command: string): ReferencingColumnBuilder;
    onUpdate(command: string): ReferencingColumnBuilder;
  };

@maximelkin
Copy link
Collaborator Author

@zeotuan first variant erases args types

@maximelkin maximelkin changed the title drop onDelete/onUpdate for ColumnBuilder fix onDelete/onUpdate for ColumnBuilder Oct 3, 2021
@zeotuan
Copy link
Contributor

zeotuan commented Oct 3, 2021

@maximelkin You are right! What do you think about this one?

 // patched ColumnBuilder methods to return ReferencingColumnBuilder with new methods
  // relies on ColumnBuilder returning only ColumnBuilder
  type ReferencingColumnBuilder = {
    [K in keyof ColumnBuilder]: (...arg: Parameters<ColumnBuilder[K]> ) => ReferencingColumnBuilder
  } & {
    inTable(tableName: string): ReferencingColumnBuilder;
    deferrable(type: deferrableType): ReferencingColumnBuilder;
    withKeyName(keyName: string): ReferencingColumnBuilder;
    onDelete(command: string): ReferencingColumnBuilder;
    onUpdate(command: string): ReferencingColumnBuilder;
  };

@maximelkin
Copy link
Collaborator Author

@zeotuan this is nice, updated PR

@kibertoad kibertoad merged commit d9d6ac8 into knex:master Oct 10, 2021
OlivierCavadenti pushed a commit to AbeonaPascha/knex that referenced this pull request Nov 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Missleading Typescript Type For onDelete on onUpdate Function

3 participants