-
Notifications
You must be signed in to change notification settings - Fork 242
/
schema.js
1195 lines (1192 loc) · 42.2 KB
/
schema.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
export default [
{
type: "heading",
size: "lg",
content: "Schema Builder",
href: "Schema"
},
{
type: "text",
content: "The `knex.schema` is a **getter function**, which returns a stateful object containing the query. Therefore be sure to obtain a new instance of the `knex.schema` for every query. These methods return [promises](https://knexjs.org/#Interfaces-Promises)."
},
{
type: "method",
method: "withSchema",
example: "knex.schema.withSchema([schemaName])",
description: "Specifies the schema to be used when using the schema-building commands.",
children: [
{
type: "runnable",
content: `
knex.schema.withSchema('public').createTable('users', function (table) {
table.increments();
})
`
}
]
},
{
type: "method",
method: "createTable",
example: "knex.schema.createTable(tableName, callback)",
description: "Creates a new table on the database, with a callback function to modify the table's structure, using the schema-building commands.",
children: [
{
type: "runnable",
content: `
knex.schema.createTable('users', function (table) {
table.increments();
table.string('name');
table.timestamps();
})
`
}
]
},
{
type: "method",
method: "createTableLike",
example: "knex.schema.createTableLike(tableName, tableNameToCopy, [callback])",
description: "Creates a new table on the database based on another table. Copy only the structure : columns, keys and indexes (expected on SQL Server which only copy columns) and not the data. Callback function can be specified to add columns in the duplicated table.",
children: [
{
type: "runnable",
content: `
knex.schema.createTableLike('new_users', 'users')
`
},
{
type: "runnable",
content: `
// "new_users" table contains columns of users and two new columns 'age' and 'last_name'.
knex.schema.createTableLike('new_users', 'users', (table) => {
table.integer('age');
table.string('last_name');
})
`
}
]
},
{
type: "method",
method: "dropTable",
example: "knex.schema.dropTable(tableName)",
description: "Drops a table, specified by tableName.",
children: [
{
type: "runnable",
content: `
knex.schema.dropTable('users')
`
}
]
},
{
type: "method",
method: "dropTableIfExists",
example: "knex.schema.dropTableIfExists(tableName)",
description: "Drops a table conditionally if the table exists, specified by tableName.",
children: [
{
type: "runnable",
content: `
knex.schema.dropTableIfExists('users')
`
}
]
},
{
type: "method",
method: "renameTable",
example: "knex.schema.renameTable(from, to)",
description: "Renames a table from a current tableName to another.",
children: [
{
type: "runnable",
content: `
knex.schema.renameTable('users', 'old_users')
`
}
]
},
{
type: "method",
method: "hasTable",
example: "knex.schema.hasTable(tableName)",
description: "Checks for a table's existence by tableName, resolving with a boolean to signal if the table exists.",
children: [
{
type: "code",
language: "js",
content: `
knex.schema.hasTable('users').then(function(exists) {
if (!exists) {
return knex.schema.createTable('users', function(t) {
t.increments('id').primary();
t.string('first_name', 100);
t.string('last_name', 100);
t.text('bio');
});
}
});
`
}
]
},
{
type: "method",
method: "hasColumn",
example: "knex.schema.hasColumn(tableName, columnName)",
description: "Checks if a column exists in the current table, resolves the promise with a boolean, true if the column exists, false otherwise.",
children: [ ]
},
{
type: "method",
method: "table",
example: "knex.schema.table(tableName, callback)",
description: "Chooses a database table, and then modifies the table, using the Schema Building functions inside of the callback.",
children: [
{
type: "runnable",
content: `
knex.schema.table('users', function (table) {
table.dropColumn('name');
table.string('first_name');
table.string('last_name');
})
`
}
]
},
{
type: "method",
method: "createView",
example: "knex.schema.createView(tableName, callback)",
description: "Creates a new view on the database, with a callback function to modify the view's structure, using the schema-building commands.",
children: [
{
type: "runnable",
content: `
knex.schema.createView('users_view', function (view) {
view.columns(['first_name']);
view.as(knex('users').select('first_name').where('age','>', '18'));
})
`
}
]
},
{
type: "method",
method: "createViewOrReplace",
example: "knex.schema.createViewOrReplace(tableName, callback)",
description: "Creates a new view or replace it on the database, with a callback function to modify the view's structure, using the schema-building commands.",
children: [
{
type: "runnable",
content: `
knex.schema.createViewOrReplace('users_view', function (view) {
view.columns(['first_name']);
view.as(knex('users').select('first_name').where('age','>', '18'));
})
`
}
]
},
{
type: "method",
method: "createMaterializedView",
example: "knex.schema.createMaterializedView(viewName, callback)",
description: "Creates a new materialized view on the database, with a callback function to modify the view's structure, using the schema-building commands. Only on PostgreSQL, CockroachDb, Redshift and Oracle.",
children: [
{
type: "runnable",
content: `
knex.schema.createMaterializedView('users_view', function (view) {
view.columns(['first_name']);
view.as(knex('users').select('first_name').where('age','>', '18'));
})
`
}
]
},
{
type: "method",
method: "refreshMaterializedView",
example: "knex.schema.refreshMaterializedView(viewName)",
description: "Refresh materialized view on the database. Only on PostgreSQL, CockroachDb, Redshift and Oracle.",
children: [
{
type: "runnable",
content: `
knex.schema.refreshMaterializedView('users_view');
`
}
]
},
{
type: "method",
method: "drowView",
example: "knex.schema.dropView(viewName)",
description: "Drop view on the database.",
children: [
{
type: "runnable",
content: `
knex.schema.dropView('users_view');
`
}
]
},
{
type: "method",
method: "dropViewIfExists",
example: "knex.schema.dropViewIfExists(viewName)",
description: "Drop view on the database if exists.",
children: [
{
type: "runnable",
content: `
knex.schema.dropViewIfExists('users_view');
`
}
]
},
{
type: "method",
method: "dropMaterializedView",
example: "knex.schema.dropMaterializedView(viewName)",
description: "Drop materialized view on the database. Only on PostgreSQL, CockroachDb, Redshift and Oracle.",
children: [
{
type: "runnable",
content: `
knex.schema.dropMaterializedView('users_view');
`
}
]
},
{
type: "method",
method: "dropMaterializedViewIfExists",
example: "knex.schema.dropMaterializedViewIfExists(viewName)",
description: "Drop materialized view on the database if exists. Only on PostgreSQL, CockroachDb, Redshift and Oracle.",
children: [
{
type: "runnable",
content: `
knex.schema.dropMaterializedViewIfExists('users_view');
`
}
]
},
{
type: "method",
method: "renameView",
example: "knex.schema.renameView(viewName)",
description: "Rename a existing view in the database. Not supported by Oracle and SQLite.",
children: [
{
type: "runnable",
content: `
knex.schema.renameView('users_view');
`
}
]
},
{
type: "method",
method: "alterView",
example: "knex.schema.alterView(viewName)",
description: "Alter view to rename columns or change default values. Only available on PostgreSQL, MSSQL and Redshift.",
children: [
{
type: "runnable",
content: `
knex.schema.alterView('view_test', function (view) {
view.column('first_name').rename('name_user');
view.column('bio').defaultTo('empty');
}
`
}
]
},
{
type: "method",
method: "generateDdlCommands",
example: "knex.schema.generateDdlCommands()",
description: "Generates complete SQL commands for applying described schema changes, without executing anything. Useful when knex is being used purely as a query builder. Generally produces same result as .toSQL(), with a notable exception with SQLite, which relies on asynchronous calls to the database for building part of its schema modification statements",
children: [
{
type: "runnable",
content: `
const ddlCommands = knex.schema.alterTable(
'users',
(table) => {
table
.foreign('companyId')
.references('company.companyId')
.withKeyName('fk_fkey_company');
}
).generateDdlCommands();
`
}
]
},
{
type: "method",
method: "raw",
example: "knex.schema.raw(statement)",
description: "Run an arbitrary sql query in the schema builder chain.",
children: [
{
type: "runnable",
content: `
knex.schema.raw(\"SET sql_mode='TRADITIONAL'\")
.table('users', function (table) {
table.dropColumn('name');
table.string('first_name');
table.string('last_name');
})
`
}
]
},
{
type: "method",
method: "queryContext",
example: "knex.schema.queryContext(context)",
href: "Schema-queryContext",
description: [
"Allows configuring a context to be passed to the [wrapIdentifier](#Installation-wrap-identifier) hook.",
"The context can be any kind of value and will be passed to `wrapIdentifier` without modification."
].join(" "),
children: [
{
type: "code",
language: "js",
content: `
knex.schema.queryContext({ foo: 'bar' })
.table('users', function (table) {
table.string('first_name');
table.string('last_name');
})
`
},
{
type: "text",
content: [
"The context configured will be passed to `wrapIdentifier`",
"for each identifier that needs to be formatted, including the table and column names.",
"However, a different context can be set for the column names via [table.queryContext](#Schema-table-queryContext)."
].join(" ")
},
{
type: "text",
content: "Calling `queryContext` with no arguments will return any context configured for the schema builder instance."
}
]
},
{
type: "method",
method: "dropSchema",
example: "knex.schema.dropSchema(schemaName, [cascade])",
description: "Drop a schema, specified by the schema's name, with optional cascade option (default to false). Only supported by PostgreSQL.",
children: [
{
type: 'code',
language: 'js',
content: `
//drop schema 'public'
knex.schema.dropSchema('public')
//drop schema 'public' cascade
knex.schema.dropSchema('public', true)
`
}
]
},
{
type: "method",
method: "dropSchemaIfExists",
example: "knex.schema.dropSchemaIfExists(schemaName, [cascade])",
description: "Drop a schema conditionally if the schema exists, specified by the schema's name, with optional cascade option (default to false). Only supported by PostgreSQL.",
children: [
{
type: 'code',
language: 'js',
content: `
//drop schema if exists 'public'
knex.schema.dropSchemaIfExists('public')
//drop schema if exists 'public' cascade
knex.schema.dropSchemaIfExists('public', true)
`
}
]
},
{
type: "heading",
size: "md",
content: "Schema Building:",
href: "Schema-Building"
},
{
type: "method",
method: "dropColumn",
example: "table.dropColumn(name)",
description: "Drops a column, specified by the column's name",
children: [ ]
},
{
type: "method",
method: "dropColumns",
example: "table.dropColumns(*columns)",
description: "Drops multiple columns, taking a variable number of column names.",
children: [ ]
},
{
type: "method",
method: "renameColumn",
example: "table.renameColumn(from, to)",
description: "Renames a column from one name to another.",
children: [ ]
},
{
type: "method",
method: "increments",
example: "table.increments(name, options={[primaryKey: boolean = true])",
description: "Adds an auto incrementing column. In PostgreSQL this is a serial; in Amazon Redshift an integer identity(1,1). This will be used as the primary key for the table. Also available is a bigIncrements if you wish to add a bigint incrementing number (in PostgreSQL bigserial). Note that a primary key is created by default, but you can override this behaviour by passing the `primaryKey` option.",
children: [
{
type: 'code',
language: 'js',
content: `
// create table 'users' with a primary key using 'increments()'
knex.schema.createTable('users', function (table) {
table.increments('userId');
table.string('name');
});
// reference the 'users' primary key in new table 'posts'
knex.schema.createTable('posts', function (table) {
table.integer('author').unsigned().notNullable();
table.string('title', 30);
table.string('content');
table.foreign('author').references('userId').inTable('users');
});
`
},
{
type: 'text',
content: "A primaryKey option may be passed, to disable to automatic primary key creation:"
}, {
type: 'code',
language: 'js',
content: `
// create table 'users' with a primary key using 'increments()'
// but also increments field 'other_id' that does not need primary key
knex.schema.createTable('users', function (table) {
table.increments('id');
table.increments('other_id', { primaryKey: false });
});
`
}
]
},
{
type: "method",
method: "integer",
example: "table.integer(name)",
description: "Adds an integer column.",
children: [ ]
},
{
type: "method",
method: "bigInteger",
example: "table.bigInteger(name)",
description: "In MySQL or PostgreSQL, adds a bigint column, otherwise adds a normal integer. Note that bigint data is returned as a string in queries because JavaScript may be unable to parse them without loss of precision.",
children: [ ]
},
{
type: "method",
method: "text",
example: "table.text(name, [textType])",
description: "Adds a text column, with optional textType for MySql text datatype preference. textType may be mediumtext or longtext, otherwise defaults to text.",
children: [ ]
},
{
type: "method",
method: "string",
example: "table.string(name, [length])",
description: "Adds a string column, with optional length defaulting to 255.",
children: [ ]
},
{
type: "method",
method: "float",
example: "table.float(column, [precision], [scale])",
description: "Adds a float column, with optional precision (defaults to 8) and scale (defaults to 2).",
children: [ ]
},
{
type: "method",
method: "double",
example: "table.double(column, [precision], [scale])",
description: "Adds a double column, with optional precision (defaults to 8) and scale (defaults to 2). In SQLite/MSSQL this is a float with no precision/scale; In PostgreSQL this is a double precision; In Oracle this is a number with matching precision/scale.",
children: [ ]
},
{
type: "method",
method: "decimal",
example: "table.decimal(column, [precision], [scale])",
description: "Adds a decimal column, with optional precision (defaults to 8) and scale (defaults to 2). Specifying NULL as precision creates a decimal column that can store numbers of any precision and scale. (Only supported for Oracle, SQLite, Postgres)",
children: [ ]
},
{
type: "method",
method: "boolean",
example: "table.boolean(name)",
description: "Adds a boolean column.",
children: [ ]
},
{
type: "method",
method: "date",
example: "table.date(name)",
description: "Adds a date column.",
children: [ ]
},
{
type: "method",
method: "datetime",
example: "table.datetime(name, options={[useTz: boolean], [precision: number]})",
description: "Adds a datetime column. By default PostgreSQL creates column with timezone (timestamptz type). This behaviour can be overriden by passing the useTz option (which is by default true for PostgreSQL). MySQL and MSSQL do not have useTz option.",
children: [{
type: 'text',
content: "A precision option may be passed:"
}, {
type: 'code',
language: 'js',
content: `table.datetime('some_time', { precision: 6 }).defaultTo(knex.fn.now(6))`
}]
},
{
type: "method",
method: "time",
example: "table.time(name, [precision])",
description: "Adds a time column, with optional precision for MySQL. Not supported on Amazon Redshift.",
children: [{
type: 'text',
content: "In MySQL a precision option may be passed:"
}, {
type: 'code',
language: 'js',
content: `table.time('some_time', { precision: 6 })`
}]
},
{
type: "method",
method: "timestamp",
example: "table.timestamp(name, options={[useTz: boolean], [precision: number]})",
description: "Adds a timestamp column. By default PostgreSQL creates column with timezone (timestamptz type) and MSSQL does not (datetime2). This behaviour can be overriden by passing the useTz option (which is by default false for MSSQL and true for PostgreSQL). MySQL does not have useTz option.",
children: [{
type: 'code',
language: 'js',
content: `table.timestamp('created_at').defaultTo(knex.fn.now());`
}, {
type: 'text',
content: "In PostgreSQL and MySQL a precision option may be passed:"
}, {
type: 'code',
language: 'js',
content: `table.timestamp('created_at', { precision: 6 }).defaultTo(knex.fn.now(6));`
}, {
type: 'text',
content: "In PostgreSQL and MSSQL a timezone option may be passed:"
}, {
type: 'code',
language: 'js',
content: `table.timestamp('created_at', { useTz: true });`
}]
},
{
type: "method",
method: "timestamps",
example: "table.timestamps([useTimestamps], [defaultToNow])",
description: "Adds created_at and updated_at columns on the database, setting each to datetime types. When true is passed as the first argument a timestamp type is used instead. Both columns default to being not null and using the current timestamp when true is passed as the second argument. Note that on MySQL the .timestamps() only have seconds precision, to get better precision use the .datetime or .timestamp methods directly with precision.",
children: [ ]
},
{
type: "method",
method: "dropTimestamps",
example: "table.dropTimestamps()",
description: "Drops the columns created_at and updated_at from the table, which can be created via timestamps.",
children: [ ]
},
{
type: "method",
method: "binary",
example: "table.binary(name, [length])",
description: "Adds a binary column, with optional length argument for MySQL.",
children: [ ]
},
{
type: "method",
href: "Schema-enum",
method: "enum / enu",
example: "table.enu(col, values, [options])",
description: "Adds a enum column, (aliased to enu, as enum is a reserved word in JavaScript). Implemented as unchecked varchar(255) on Amazon Redshift. Note that the second argument is an array of values. Example:",
children: [{
type: 'code',
language: 'js',
content: `table.enu('column', ['value1', 'value2'])`
}, {
type: 'text',
content: "For Postgres, an additional options argument can be provided to specify whether or not to use Postgres's native TYPE:"
}, {
type: 'code',
language: 'js',
content: `table.enu('column', ['value1', 'value2'], { useNative: true, enumName: 'foo_type' })`
}, {
type: 'text',
content: "It will use the values provided to generate the appropriate TYPE. Example:"
}, {
type: 'code',
language: 'sql',
content: `CREATE TYPE "foo_type" AS ENUM ('value1', 'value2');`
}, {
type: 'text',
content: "To use an existing native type across columns, specify 'existingType' in the options (this assumes the type has already been created):",
}, {
type: 'info',
content: "Note: Since the enum values aren't utilized for a native && existing type, the type being passed in for values is immaterial."
}, {
type: 'code',
language: 'js',
content: `table.enu('column', null, { useNative: true, existingType: true, enumName: 'foo_type' })`
}, {
type: 'text',
content: "If you want to use existing enums from a schema, different from the schema of your current table, specify 'schemaName' in the options:"
}, {
type: 'code',
language: 'js',
content: `table.enu('column', null, { useNative: true, existingType: true, enumName: 'foo_type', schemaName: 'public' })`
}
]
},
{
type: "method",
method: "json",
example: "table.json(name)",
children: [{
type: 'text',
content: `Adds a json column, using the built-in json type in PostgreSQL, MySQL and SQLite, defaulting to a text column in older versions or in unsupported databases.`
}, {
type: 'text',
content: `For PostgreSQL, due to incompatibility between native array and json types, when setting an array (or a value that could be an array) as the value of a json or jsonb column, you should use JSON.stringify() to convert your value to a string prior to passing it to the query builder, e.g.`
}, {
type: 'code',
language: 'js',
content: `
knex.table('users')
.where({id: 1})
.update({json_data: JSON.stringify(mightBeAnArray)});
`
}
]
},
{
type: "method",
method: "jsonb",
example: "table.jsonb(name)",
description: "Adds a jsonb column. Works similar to table.json(), but uses native jsonb type if possible.",
children: [ ]
},
{
type: "method",
method: "uuid",
example: "table.uuid(name)",
description: "Adds a uuid column - this uses the built-in uuid type in PostgreSQL, and falling back to a char(36) in other databases.",
children: [ ]
},
{
type: "method",
method: "comment",
example: "table.comment(value)",
description: "Sets the comment for a table.",
children: [ ]
},
{
type: "method",
method: "engine",
example: "table.engine(val)",
description: "Sets the engine for the database table, only available within a createTable call, and only applicable to MySQL.",
children: [ ]
},
{
type: "method",
method: "charset",
example: "table.charset(val)",
description: "Sets the charset for the database table, only available within a createTable call, and only applicable to MySQL.",
children: [ ]
},
{
type: "method",
method: "collate",
example: "table.collate(val)",
description: "Sets the collation for the database table, only available within a createTable call, and only applicable to MySQL.",
children: [ ]
},
{
type: "method",
method: "inherits",
example: "table.inherits(val)",
description: "Sets the tables that this table inherits, only available within a createTable call, and only applicable to PostgreSQL.",
children: [ ]
},
{
type: "method",
method: "specificType",
example: "table.specificType(name, type)",
description: "Sets a specific type for the column creation, if you'd like to add a column type that isn't supported here.",
children: [ ]
},
{
type: "method",
method: "index",
example: "table.index(columns, [indexName], options=({[indexType: string], [storageEngineIndexType: 'btree'|'hash'], [predicate: QueryBuilder]}))",
description: "Adds an index to a table over the given columns. A default index name using the columns is used unless indexName is specified. In MySQL, the storage engine index type may be 'btree' or 'hash' index types, more info in Index Options section : https://dev.mysql.com/doc/refman/8.0/en/create-index.html. The indexType can be optionally specified for PostgreSQL and MySQL. Amazon Redshift does not allow creating an index. In PostgreSQL, SQLite and MSSQL a partial index can be specified by setting a 'where' predicate.",
children: [
{
type: 'code',
language: 'js',
content: `
knex.table('users', function (table) {
table.index(['name', 'last_name'], 'idx_name_last_name', {
indexType: 'FULLTEXT',
storageEngineIndexType: 'hash',
predicate: knex.whereNotNull('email'),
});
});
`
}
]
},
{
type: "method",
method: "dropIndex",
example: "table.dropIndex(columns, [indexName])",
description: "Drops an index from a table. A default index name using the columns is used unless indexName is specified (in which case columns is ignored). Amazon Redshift does not allow creating an index.",
children: [ ]
},
{
type: "method",
method: "setNullable",
example: "table.setNullable(column)",
description: "Makes table column nullable.",
children: [ ]
},
{
type: "method",
method: "dropNullable",
example: "table.dropNullable(column)",
description: "Makes table column not nullable. Note that this operation will fail if there are already null values in this column.",
children: [ ]
},
{
type: "method",
method: "primary",
href:"Schema-table-primary",
example: "table.primary(columns, options=({[constraintName:string],[deferrable:'not deferrable'|'deferred'|'immediate']})",
description: "Create a primary key constraint on table using input `columns`. If you need to create a composite primary key, pass an array of columns to `columns`. Constraint name defaults to `tablename_pkey` unless `constraintName` is specified. On Amazon Redshift, all columns included in a primary key must be not nullable. Deferrable primary constraint are supported on Postgres and Oracle and can be set by passing deferrable option to options object.",
children: [
{
type: 'code',
language: 'js',
content: `
knex.schema.alterTable('users', function(t) {
t.unique('email')
})
knex.schema.alterTable('job', function(t) {
t.primary('email',{constraintName:'users_primary_key',deferrable:'deferred'})
})
`
},
{
type: "info",
content: "Note: If you want to chain primary() while creating new column you can use <a href='#Schema-column-primary'>primary</a>"
}
]
},
{
type: "method",
method: "unique",
href:"Schema-table-unique",
example: "table.unique(columns, options={[indexName: string], [deferrable:'not deferrable'|'immediate'|'deferred'], [storageEngineIndexType:'btree'|'hash']})",
description: "Adds an unique index to a table over the given `columns`. In MySQL, the storage engine index type may be 'btree' or 'hash' index types, more info in Index Options section : https://dev.mysql.com/doc/refman/8.0/en/create-index.html. A default index name using the columns is used unless indexName is specified. If you need to create a composite index, pass an array of column to `columns`. Deferrable unique constraint are supported on Postgres and Oracle and can be set by passing deferrable option to options object.",
children: [
{
type: 'code',
language: 'js',
content: `
knex.schema.alterTable('users', function(t) {
t.unique('email')
})
knex.schema.alterTable('job', function(t) {
t.unique(['account_id', 'program_id'], {indexName: 'users_composite_index', deferrable:'deferred', storageEngineIndexType: 'hash'})
})
`
},
{
type: "info",
content: "Note: If you want to chain unique() while creating new column you can use <a href='#Schema-column-unique'>unique</a>"
}
]
},
{
type: "method",
method: "foreign",
example: "table.foreign(columns, [foreignKeyName])[.onDelete(statement).onUpdate(statement).withKeyName(foreignKeyName).deferrable(type)]",
description: "Adds a foreign key constraint to a table for an existing column using `table.foreign(column).references(column)` or multiple columns using `table.foreign(columns).references(columns).inTable(table)`.",
children: [
{
type: "text",
content: [
"A default key name using the columns is used unless `foreignKeyName` is specified.",
"You can also chain `onDelete()` and/or `onUpdate()` to set the reference option `(RESTRICT, CASCADE, SET NULL, NO ACTION)` for the operation. You can also chain `withKeyName()` to override default key name that is generated from table and column names (result is identical to specifying second parameter to function `foreign()`).",
"Deferrable foreign constraint is supported on Postgres and Oracle and can be set by chaining `.deferrable(type)`",
"Note that using `foreign()` is the same as `column.references(column)` but it works for existing columns."
]
},
{
type: 'code',
language: 'js',
content: `
knex.schema.table('users', function (table) {
table.integer('user_id').unsigned()
table.foreign('user_id').references('Items.user_id_in_items').deferrable('deferred')
})`
}
]
},
{
type: "method",
method: "dropForeign",
example: "table.dropForeign(columns, [foreignKeyName])",
description: "Drops a foreign key constraint from a table. A default foreign key name using the columns is used unless foreignKeyName is specified (in which case columns is ignored).",
children: [ ]
},
{
type: "method",
method: "dropUnique",
example: "table.dropUnique(columns, [indexName])",
description: "Drops a unique key constraint from a table. A default unique key name using the columns is used unless indexName is specified (in which case columns is ignored).",
children: [ ]
},
{
type: "method",
method: "dropPrimary",
example: "table.dropPrimary([constraintName])",
description: "Drops the primary key constraint on a table. Defaults to tablename_pkey unless constraintName is specified.",
children: [ ]
},
{
type: "method",
method: "queryContext",
example: "table.queryContext(context)",
href: "Schema-table-queryContext",
description: [
"Allows configuring a context to be passed to the [wrapIdentifier](#Installation-wrap-identifier) hook for formatting table builder identifiers.",
"The context can be any kind of value and will be passed to `wrapIdentifier` without modification."
].join(" "),
children: [
{
type: "code",
language: "js",
content: `
knex.schema.table('users', function (table) {
table.queryContext({ foo: 'bar' });
table.string('first_name');
table.string('last_name');
})
`
},
{
type: "text",
content: "This method also enables overwriting the context configured for a schema builder instance via [schema.queryContext](#Schema-queryContext):"
},
{
type: "code",
language: "js",
content: `
knex.schema.queryContext('schema context')
.table('users', function (table) {
table.queryContext('table context');
table.string('first_name');
table.string('last_name');
})
`
},
{
type: "text",
content: "Note that it's also possible to overwrite the table builder context for any column in the table definition:"
},
{
type: "code",
language: "js",
content: `
knex.schema.queryContext('schema context')
.table('users', function (table) {
table.queryContext('table context');
table.string('first_name').queryContext('first_name context');
table.string('last_name').queryContext('last_name context');
})
`
},
{
type: "text",
content: "Calling `queryContext` with no arguments will return any context configured for the table builder instance."
}
]
},
{
type: "heading",
size: "md",
content: "Chainable Methods:",
href: "Chainable"
},
{
type: "text",
content: "The following three methods may be chained on the schema building methods, as modifiers to the column."
},
{
type: "method",
method: "alter",
example: "column.alter()",
description: 'Marks the column as an alter / modify, instead of the default add. Note: This only works in .alterTable() and is not supported by SQlite or Amazon Redshift. Alter is *not* done incrementally over older column type so if you like to add `notNullable` and keep the old default value, the alter statement must contain both `.notNullable().defaultTo(1).alter()`. If one just tries to add `.notNullable().alter()` the old default value will be dropped.',
children: [ ]
},
{
type: "code",
content: `
knex.schema.alterTable('user', function(t) {
t.increments().primary(); // add
// drops previous default value from column, change type to string and add not nullable constraint
t.string('username', 35).notNullable().alter();
// drops both not null constraint and the default value
t.integer('age').alter();
});
`
},
{
type: "method",
method: "index",
example: "column.index([indexName], options=({[indexType: string], [storageEngineIndexType: 'btree'|'hash'], [predicate: QueryBuilder]}))",
description: "Specifies a field as an index. If an indexName is specified, it is used in place of the standard index naming convention of tableName_columnName. In MySQL, the storage engine index type may be 'btree' or 'hash' index types, more info in Index Options section : https://dev.mysql.com/doc/refman/8.0/en/create-index.html. The indexType can be optionally specified for PostgreSQL and MySQL. No-op if this is chained off of a field that cannot be indexed. In PostgreSQL, SQLite and MSSQL a partial index can be specified by setting a 'where' predicate.",
children: [ ]
},
{
type: "method",
method: "primary",
href:"Schema-column-primary",
example: "column.primary(options=({[constraintName:string],[deferrable:'not deferrable'|'deferred'|'immediate']}));",
description: "Sets a primary key constraint on `column`. Constraint name defaults to `tablename_pkey` unless `constraintName` is specified. On Amazon Redshift, all columns included in a primary key must be not nullable. Deferrable primary constraint are supported on Postgres and Oracle and can be set by passing deferrable option to options object.",
children: [
{
type: 'code',
language: 'js',
content: `
knex.schema.table('users', function (table) {
table.integer('user_id').primary('email',{constraintName:'users_primary_key',deferrable:'deferred'})