sqly 0.4.0

A lightweight macro system on top of sqlx
Documentation
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
# Attribute Documentation

##### Struct Attributes:
`#[sqly(`[`table`](#table)`,`[`rename`](#rename)`)]`<br>
`#[sqly(`[`from_row`](#from_row)`,`[`from_flat`](#flat)`,`[`flat_row`](#flat)`)]`<br>
`#[sqly(`[`flat`](#flat)`,`[`delete`](#delete)`,`[`insert`](#insert)`,`[`select`](#select)`,`[`update`](#update)`,`[`derive`](#derive)`,`[`visibility`](#visibility)`)]`<br>
`#[sqly(`[`dynamic`](#dynamic)`,`[`optional`](#optional)`,`[`serde_double_option`](#serde_double_option)`,`[`filter`](#filter)`,`[`returning`](#returning)`)]`<br>
`#[sqly(`[`crate`](#dev-attributes)`,`[`unchecked`](#dev-attributes)`,`[`print`](#dev-attributes)`,`[`debug`](#dev-attributes)`)]`<br>

##### Field Attributes:
`#[sqly(`[`column`](#column)`,`[`rename`](#rename)`)]`<br>
`#[sqly(`[`select`](#select-1)`,`[`insert`](#insert-1)`,`[`update`](#update-1)`)]`<br>
`#[sqly(`[`optional`](#optional)`,`[`filter`](#filter)`,`[`value`](#value)`,`[`infer`](#infer)`)]`<br>
`#[sqly(`[`foreign`](#foreign)`,`[`target`](#target)`,`[`named`](#named)`,`[`typed`](#typed)`)]`<br>
`#[sqly(`[`default`](#default)`,`[`from`](#from)`)]`<br>
`#[sqly(`[`skip`](#skip)`,`[`key`](#key)`)]`<br>


<br>

## Struct Attributes

<br>

#### table
----
```
# #[derive(sqly::Table)]
#[sqly(table = "string")]
# struct T;
```
The name of the database table which this type represents.

All uses of the table name will be enclosed in quotes.

----
```
# mod path {
# #[derive(sqly::Table)]
# #[sqly(table = "")]
# pub struct Type;
# }
# #[derive(sqly::Delete)]
#[sqly(table = path::Type)]
# struct T { t: i32 }
```
The path to the type representing the table to be operated on.

This type is required to have [`#[derive(Table)]`](derive@Table).

<br>

#### from_row
---
```
# #[derive(sqly::Table)]
# #[sqly(table = "")]
#[sqly(from_row)]
# struct T;
```
Implements [`sqlx::FromRow`] for the table.

This is the default behavior if [`#[sqly(select)]`](#select) is specified.

Required to use this table with [`#[derive(Select)]`](derive@Select) or [`#[sqly(returning)]`](#returning) queries.

<br>

#### flat
---
```
# #[derive(sqly::Table)]
# #[sqly(table = "")]
#[sqly(flat)]
# struct T;
```
Generate the flattened struct representation of this table.

This excludes all skipped fields and matches the SQL `SELECT` list.

The struct is named by `format_ident!("Flat{}", self.ident)`.

---
```
# #[derive(sqly::Table)]
# #[sqly(table = "")]
#[sqly(flat = Ident)]
# struct T;
```
Same as above, except the struct is named by the given `Ident`.

---
```
# #[derive(sqly::Table)]
# #[sqly(table = "", flat)]
#[sqly(from_flat)]
# struct T;
```
Implements [`From<Self::Flat>`](Flat::Flat) for the table.

---
```
# #[derive(sqly::Table)]
# #[sqly(table = "", flat)]
#[sqly(flat_row)]
# struct T;
```
Implements [`sqlx::FromRow`] for the flattened struct.

This can also be done with [`#[sqly(flat_derive = sqlx::FromRow)]`](#derive), but this will generate `non_snake_case` warnings due to a different implementation of the function body.

<br>

#### delete
---
```
# #[derive(sqly::Table)]
# #[sqly(table = "")]
#[sqly(delete)]
# struct T { #[sqly(key)] t: i32 }
```
Generate a delete struct with [`#[derive(Delete)]`](derive@Delete) applied.

Only fields which are marked as [`#[sqly(key)]`](#key) will be included in the generated struct.

The struct is named by `format_ident!("Delete{}", self.ident)`.

---
```
# #[derive(sqly::Table)]
# #[sqly(table = "")]
#[sqly(delete = Ident)]
# struct T { #[sqly(key)] t: i32 }
```
Same as above, except the struct is named by the given `Ident`.

<br>

#### insert
---
```
# #[derive(sqly::Table)]
# #[sqly(table = "")]
#[sqly(insert)]
# struct T { t: i32 }
```
Generate an insert struct with [`#[derive(Insert)]`](derive@Insert) applied.

All fields which are not marked as [`#[sqly(skip)]`](#skip) will be included in the generated struct.

The struct is named by `format_ident!("Insert{}", self.ident)`.

---
```
# #[derive(sqly::Table)]
# #[sqly(table = "")]
#[sqly(insert = Ident)]
# struct T { t: i32 }
```
Same as above, except the struct is named by the given `Ident`.

<br>

#### select
---
```
# #[derive(sqly::Table)]
# #[sqly(table = "")]
#[sqly(select)]
# struct T;
```
Generate a select struct with [`#[derive(Select)]`](derive@Select) applied.

Only fields which are marked as [`#[sqly(key)]`](#key) will be included in the generated struct.

The struct is named by `format_ident!("Select{}", self.ident)`.

---
```
# #[derive(sqly::Table)]
# #[sqly(table = "")]
#[sqly(select = Ident)]
# struct T;
```
Same as above, except the struct is named by the given `Ident`.

<br>

#### update
---
```
# #[derive(sqly::Table)]
# #[sqly(table = "")]
#[sqly(update)]
# struct T { #[sqly(key)] k: i32, v: i32 }
```
Generate an update struct with [`#[derive(Update)]`](derive@Update) applied.

All fields which are not marked as [`#[sqly(skip)]`](#skip) will be included in the generated struct.

The struct is named by `format_ident!("Update{}", self.ident)`.

---
```
# #[derive(sqly::Table)]
# #[sqly(table = "")]
#[sqly(update = Ident)]
# struct T { #[sqly(key)] k: i32, v: i32 }
```
Same as above, except the struct is named by the given `Ident`.

<br>

#### derive
---
```
# #[derive(sqly::Table)]
# #[sqly(table = "")]
#[sqly(query_derive = Clone)]
#[sqly(query_derive = Debug, Display)]
# struct T;
```
Add the provided derive macros to all generated query structs.

Multiple instances of this attribute are joined into a single list.

---
```
# #[derive(sqly::Table)]
# #[sqly(table = "", delete, insert)]
# #[sqly(query_derive = Clone)]
#[sqly(delete_derive = Copy)]
#[sqly(insert_derive = PartialEq, Eq)]
# struct T { #[sqly(key)] t: i32 }
```
Add the provided derive macros to the specified generated struct.

Multiple instances of this attribute are joined into a single list.

This appends to the values set with `query_derive`.

<br>

#### visibility
---
```
# #[derive(sqly::Table)]
# #[sqly(table = "")]
#[sqly(query_visibility = pub(crate))]
# struct T;
```
Set the visbility of all generated query structs.

If not specified this defaults to the visibility of the current struct.

---
```
# #[derive(sqly::Table)]
# #[sqly(table = "", delete, insert)]
#[sqly(flat_visibility = pub)]
#[sqly(delete_visibility = ,)]
# pub struct T { #[sqly(key)] t: i32 };
```
Set the visbility of the specified generated struct.

This overrides the value set with `query_visibility`.

Use `visibility = ,` to set an inherited (private) visibility.

<br>

#### returning
---
```
# #[derive(sqly::Table)]
# #[sqly(table = "")]
#[sqly(returning)]
# struct T;
```
Returns this table through the SQL `RETURNING` clause in generated queries.

Generates the same output list and uses the same [`sqlx::FromRow`] definition as [`#[derive(Select)]`](#derive@Select).

When not applied to [`#[derive(Table)]`](derive@Table) the type specified with [`#[sqly(table)]`](#table) will be returned instead.

---
```
# #[derive(sqly::Table)]
# #[sqly(table = "")]
#[sqly(returning = Table)]
# struct T;
```
Same as above, except the path to the table to be returned is specified.

This type is required to have [`#[derive(Table)]`](derive@Table).

---
```
# #[derive(sqly::Table)]
# #[sqly(table = "")]
#[sqly(returning = { field, other })]
# struct T;
```
Returns the specified fields as a scalar or tuple through the SQL `RETURNING` clause in generated queries.

The fields must match an identifier of the fields in either this struct or the type specified with [`#[sqly(table)]`](#table).

Unlike [`#[sqly(returning)]`](#returning) for tables, the [`#[sqly(default)]`](#default) and [`#[sqly(from)]`](#from) attributes are not applied.

---
```
# #[derive(sqly::Table)]
# #[sqly(table = "")]
#[sqly(returning = Table { field, other })]
# struct T;
```
Same as above, except the path to the table with the fields is specified.

This type is required to have [`#[derive(Table)]`](derive@Table).

---
```
# #[derive(sqly::Table)]
# #[sqly(table = "", insert, update, from_row)]
#[sqly(insert_returning = { field })]
#[sqly(update_returning = Self)]
# struct T { #[sqly(key)] k: i32, field: i32 }
```
Same as all of the above, except only for the operations specified.

This overrides the value set with `returning`.

<br>

#### dynamic
---
```
# #[derive(sqly::Table)]
# #[sqly(table = "", optional, insert)]
#[sqly(dynamic)]
# struct T { t: i32 }
```
Queries with [`#[sqly(optional)]`](#optional) must be generated at runtime, this requires storing the SQL in an owned string and prevents the use of the [`Delete`](Delete), [`Insert`](Insert), [`Select`](Select) and [`Update`](Update) traits (the corresponding `Impl` traits must be used instead). Additionally, the implementation generated for the `Check` traits only tests for the case of all optional fields being provided, possibly allowing additional runtime query errors to occur (although this should not make any difference in practice).

[`#[sqly(dynamic)]`](#dynamic) must be used to explicitly opt-in for this behavior.

<br>

#### serde_double_option
---
```
# #[derive(sqly::Table)]
# #[sqly(table = "", optional)]
#[sqly(serde_double_option)]
# struct T;
```
Adds a [serde attribute](https://serde.rs/field-attrs.html) to every field generated with a type wrapped in two `Option`s.

```text
#[serde(default, with = "sqly::double_option", skip_serializing_if = "Option::is_none")]
```

Will likely generate errors if a [serde derive](https://serde.rs/derive.html) is not specified with [`#[sqly(query_derive)]`](#derive).

Intended but not required to be used together with [`#[sqly(optional)]`](#optional).

See [serde_with](https://docs.rs/serde_with/3.12.0/serde_with/rust/double_option/index.html) for more information.

<br>

#### dev-attributes
---
```
# #[derive(sqly::Table)]
# #[sqly(table = "")]
#[sqly(crate = ::sqly)]
# struct T;
```
Specify the path to the `sqly` crate instance to use in generated code.

---
```
# #[derive(sqly::Table)]
# #[sqly(table = "")]
#[sqly(unchecked)]
# struct T;
```
Disable compile time checking for all queries generated by this derive.

This has no effect if the default [`checked`](https://github.com/LMOORS30/sqly#features) feature is not enabled.

---
```
# #[derive(sqly::Table)]
# #[sqly(table = "")]
#[sqly(print)]
# struct T;
```
Prints generated queries to stdout at compile time.

Intended use: `cargo check > queries.txt`.

Intended for development only.

---
```rust,ignore
#[sqly(debug)]
```
Prints generated code to stdout at compile time.

Intended use: `cargo check > generated.rs`.

Intended for development only.

This example is not tested in order to avoid build output in tests. lol

<br>
<br>
<br>

## Field Attributes

<br>

#### column
---
```
# #[derive(sqly::Table)]
# #[sqly(table = "")]
# struct T {
#[sqly(column = "string")]
# t: i32,
# }
```
The name of the database column which this field maps to.

If not specified this defaults to the ident of the field.

All uses of the column name will be enclosed in quotes.

Includes support for the sqlx&ensp;`?`&ensp;`!`&ensp;`: _`&ensp;and&ensp;`: T`&ensp;[type overrides](sqlx::query!#type-overrides-output-columns).

<br>

#### rename
---
```
# #[derive(sqly::Table)]
# #[sqly(table = "")]
# struct T {
#[sqly(rename = "UPPERCASE")]
# t: i32,
# }
```
Rename columns according to the given naming convention.

One of:<br>
`"none"`<br>
`"lowercase"`<br>
`"UPPERCASE"`<br>
`"camelCase"`<br>
`"PascalCase"`<br>
`"snake_case"`<br>
`"kebab-case"`<br>
`"SCREAMING_SNAKE_CASE"`<br>
`"SCREAMING-KEBAB-CASE"`

This will rename the column regardless of whether it was specified with [`#[sqly(column)]`](#column).

This attribute can be applied to both the struct and its fields, where the value in fields overrides the value in the struct.

<br>

#### select
---
```
# #[derive(sqly::Table)]
# #[sqly(table = "")]
# struct T {
#[sqly(select = "$table.$column")]
# t: i32,
# }
```
The SQL expression to select the column for this field.

This [Variadic Attribute](docs::attr::note#variadic-attributes) supports [String Placeholders](docs::attr::note#string-placeholders), and they are necessary to generate valid queries.

The table must be referenced as `$table`, the column can optionally be referenced as `$column`, the alias must not be set.

No other string placeholders are available, other tables and parameter bindings can currently not be referenced.

<br>

#### insert
---
```
# #[derive(sqly::Insert)]
# #[sqly(table = "")]
# struct T {
#[sqly(insert = "$i")]
# t: i32,
# }
```
The SQL expression to insert the column for this field.

This [Variadic Attribute](docs::attr::note#variadic-attributes) supports [String Placeholders](docs::attr::note#string-placeholders), and they are necessary to generate valid queries.

The value bound by this field must be referenced as `$i`, values bound by other fields can be referenced by their ident.

Any field can be referenced any amount of times, including skipped fields, or not at all.

<br>

#### update
---
```
# #[derive(sqly::Update)]
# #[sqly(table = "")]
# struct T {
#[sqly(update = "$i")]
# t: i32,
# #[sqly(key)] k: i32,
# }
```
The SQL expression to update the column for this field.

This [Variadic Attribute](docs::attr::note#variadic-attributes) supports [String Placeholders](docs::attr::note#string-placeholders), and they are necessary to generate valid queries.

The value bound by this field must be referenced as `$i`, the column can optionally be referenced as `$column`, values bound by other fields can be referenced by their ident.

Any field can be referenced any amount of times, including skipped and keyed fields, or not at all.

<br>

#### filter
---
```
# #[derive(sqly::Delete)]
# #[sqly(table = "")]
# struct T {
#[sqly(filter = "$column = $i")]
# t: i32,
# }
```
The SQL condition to filter in the `WHERE` clause generated by queries.

This [Variadic Attribute](docs::attr::note#variadic-attributes) supports [String Placeholders](docs::attr::note#string-placeholders), and they are necessary to generate valid queries.

The value bound by this field must be referenced as `$i`, the column can optionally be referenced as `$column`, values bound by other fields can be referenced by their ident.

This attribute can be applied to both the struct and its fields, all of which are generated and evaluated with `AND` operators.

Any field can be referenced any amount of times, including skipped fields, or not at all.

---
```
# #[derive(sqly::Table)]
# #[sqly(table = "", delete, select)]
# struct T {
# #[sqly(key)]
#[sqly(delete_filter = "$column = $i")]
#[sqly(select_filter = "$column = $i")]
# t: i32,
# }
```
Same as above, except only for the operations specified.

This overrides the value set with `filter`.

<br>

#### optional
---
```
# #[derive(sqly::Table)]
# #[sqly(table = "", dynamic, insert)]
#[sqly(optional)]
# struct T {
# t: i32,
# }
```
Declares a field or set of fields as optional.

An optional field will only be included in the generated query if its runtime value resolves to an `Option::Some`, otherwise it will behave as if it was skipped. Will generate type errors if the value bound does not resolve to an `Option`. Does not affect the SQL `SELECT` list, [`sqlx::FromRow`] definition and [`Flat`](#flat) struct, as these do not involve runtime values before execution.

Any field can be optional, even those with [`#[sqly(value)]`](#value) or [`#[sqly(filter)]`](#filter), among others. The value to be bound by this field determines whether the part of the query relevant to this field is included. This check happens regardless of whether the field binds its own value (e.g. [`#[sqly(insert = "default")]`](#insert-1)) or others (e.g. [`#[sqly(update = "COALESCE($i, $j)")]`](#update-1)).

This attribute can be applied to both the struct and its fields. When applied to the struct its behavior is different depening on the derive. [`#[derive(Table)]`](derive@Table) will set all fields as optional, additionally, it will wrap all optional fields in an `Option` when included in generated [`Delete`](derive@Delete), [`Insert`](derive@Insert), [`Select`](derive@Select) and [`Update`](derive@Update) structs. Other derives will only set fields whose type is already wrapped in an `Option` as optional, but fields can be individually specified as optional regardless of their type.

[`#[sqly(dynamic)]`](#dynamic) is required to explicitly opt-in for this behavior.

---
```
# #[derive(sqly::Table)]
# #[sqly(table = "", dynamic, insert)]
#[sqly(optional = keys)]
# struct T {
# #[sqly(key)]
# t: i32,
# }
```
Apply optional to all fields which are marked as a key.

---
```
# #[derive(sqly::Table)]
# #[sqly(table = "", dynamic, insert)]
#[sqly(optional = values)]
# struct T {
# t: i32,
# }
```
Apply optional to all fields which are not marked as a key.

---
```
# #[derive(sqly::Table)]
# #[sqly(table = "", dynamic, optional, insert)]
# struct T {
#[sqly(optional = false)]
# t: i32,
# v: i32,
# }
```
Overrides any value set on the struct and disables optional for this field.

---
```
# #[derive(sqly::Table)]
# #[sqly(table = "", dynamic, insert, update)]
#[sqly(insert_optional = keys)]
# struct T {
# t: i32,
#[sqly(update_optional = false)]
# #[sqly(key)] k: i32,
# }
```
Same as all of the above, except only for the operations specified.

This overrides the value set with `optional`.

<br>

#### value
---
```
# #[derive(sqly::Table)]
# #[sqly(table = "", insert)]
# struct T {
#[sqly(value = self.field)]
# field: i32
# }
```
The Rust expression to bind this field as a parameter.

A reference to the instance of this object is assigned to `self`.

Includes support for the sqlx&ensp;`as _`&ensp;[type override](sqlx::query!#type-overrides-bind-parameters-postgres-only).

<br>

#### infer
---
```
# #[derive(sqly::Table)]
# #[sqly(table = "")]
# struct T {
#[sqly(infer)]
# t: i32,
# }
```
Disable type compatibility checking for this field, does not influence nullability checks.

This is a shorthand for the [`#[sqly(column)]`](#column) and [`#[sqly(value)]`](#value) type overrides.

The following attributes achieve the same effect:

`#[sqly(column = "column: _")]`<br>
`#[sqly(value = value as _)]`

<br>

#### foreign
---
```
# #[derive(sqly::Table)]
# #[sqly(table = "")]
# struct T {
#[sqly(foreign)]
# t: i32,
# }
```
Mark this field as a foreign table.

This attribute has several implications:

When generating `SELECT` queries an SQL `JOIN` expression is added for the foreign table. Additionally, all columns needed for the [`sqlx::FromRow`] implementation of the foreign struct are selected. This works recursively and for any amount of foreign tables. Joined tables and selected columns are renamed in order to avoid name conflicts.

The type of this field is required to have [`#[derive(Table)]`](derive@Table) and must be a path without any generics. The only exception is `Option<T>`, where the same restrictions apply to `T` and the identifier of `Option` must not be renamed. This prompts the generated expression to perform a `LEFT JOIN` instead of an `INNER JOIN`.

When generating [`Delete`](derive@Delete), [`Insert`](derive@Insert), [`Select`](derive@Select) and [`Update`](derive@Update) structs this field will have its ident and type changed in order to match the column used in the SQL `JOIN` expression. When generating the [`Table::Flat`](#flat) struct all fields are recursively flattened and renamed in order to match the SQL `SELECT` list.

The [`#[sqly(target)]`](#target), [`#[sqly(named)]`](#named) and [`#[sqly(typed)]`](#typed) attributes further explain this behavior.

---
```
# #[derive(sqly::Table)]
# #[sqly(table = "")]
# struct T {
#[sqly(foreign = "$INNER JOIN other AS $other ON $other.id = $table.other_id")]
# t: i32,
# }
```
Set a custom SQL `JOIN` expression for the foreign table.

This [Variadic Attribute](docs::attr::note#variadic-attributes) supports [String Placeholders](docs::attr::note#string-placeholders), and they are necessary to generate valid queries.

The joined table must be renamed to `$other` and the current table must be referenced as `$table`.

Joins should be specified with one of `$INNER`, `$inner`, `$LEFT`, `$left` to support further `JOIN`s on this table.

Other tables can be referenced by their unique alias relative to the current scope (e.g. `$table_name`).

**Note**<br>
Foreign fields may be included in generated [`Delete`](derive@Delete), [`Insert`](derive@Insert), [`Select`](derive@Select) and [`Update`](derive@Update) structs.

When performing a custom SQL `JOIN` this is likely to cause errors due to incorrect default attribute values.

Either specify the relevant attributes ([`#[sqly(column)]`](#column), [`#[sqly(named)]`](#named), [`#[sqly(typed)]`](#named)), or exclude this field from generated structs ([`#[sqly(skip = delete, insert, select, update)]`](#skip)).

**Warning**<br>
Nullability is not checked for SQL `JOIN`s. When performing a `LEFT JOIN` be sure to set the type of this field to an `Option`.

<br>

#### target
---
```
# #[derive(sqly::Table)]
# #[sqly(table = "")]
# struct T {
# #[sqly(foreign)]
#[sqly(target = field)]
# t: i32,
# }
```
Specify the ident of the field in the foreign struct to perform the SQL `JOIN` on.

The generated SQL `JOIN` will check for equality between the column of this field and the column of the chosen target field.

If not specified a default field is chosen only if there is exactly one key field in the foreign struct, otherwise an error will be raised.

---
```
# #[derive(sqly::Table)]
# #[sqly(table = "")]
# struct T {
# #[sqly(foreign)]
#[sqly(target = "column")]
# t: i32,
# }
```
Specify the column of the foreign table to perform the SQL `JOIN` on.

This is the same as above, except the column is specified directly and not required to exist in the foreign struct.

<br>

#### named
---
```
# #[derive(sqly::Table)]
# #[sqly(table = "")]
# struct T {
# #[sqly(foreign)]
#[sqly(named = ident)]
# t: i32,
# }
```
Set the ident of this field when included in generated [`Delete`](derive@Delete), [`Insert`](derive@Insert), [`Select`](derive@Select) and [`Update`](derive@Update) structs.

When used with [`#[sqly(foreign)]`](#foreign) this is supposed to match the name of the column used in the SQL `JOIN` expression.

For foreign fields a new ident is required, if not specified a default ident is constructed by a series of rules:
1. If [`#[sqly(column)]`](#column) is specified, the ident is set by `format_ident!("{}", self.column.to_snake_case())`.
2. If a target field is found, the ident is set by `format_ident!("{}_{}", self.ident, foreign.ident)`.
3. Otherwise, the ident is set by `format_ident!("{}_{}", self.ident, target.to_snake_case())`.

This can cause problems if [`#[sqly(column)]`](#column) is also not specified, as the constructed default ident will now be used to determine the column name, and this must match the column used in the SQL `JOIN`. If the constructed default ident does not match the name of the column, and no explicit column is specified, the resulting query will be wrong. This can be resolved by specifying either [`#[sqly(column)]`](#column) or [`#[sqly(named)]`](#named).

<br>

#### typed
---
```
# #[derive(sqly::Table)]
# #[sqly(table = "")]
# struct T {
# #[sqly(foreign)]
#[sqly(typed = Type)]
# t: i32,
# }
```
Set the type of this field when included in generated [`Delete`](derive@Delete), [`Insert`](derive@Insert), [`Select`](derive@Select) and [`Update`](derive@Update) structs.

When used with [`#[sqly(foreign)]`](#foreign) this is supposed to match the type of the column used in the SQL `JOIN` expression.

For foreign fields a new type is required, if not specified this is set to the type of the target field, otherwise an error will be raised.

Overrides any type modifications made by [`#[sqly(from)]`](#from), [`#[sqly(default)]`](#default) and [`#[sqly(optional)]`](#optional).

<br>

#### default
---
```
# #[derive(sqly::Table)]
# #[sqly(table = "")]
# struct T {
#[sqly(default)]
# t: i32,
# }
```
Decode this field by using its `Default` implementation for `NULL` values in the query result.

This attribute can be used along with [`#[sqly(foreign)]`](#foreign), in which case a `LEFT JOIN` will be performed.

This will wrap the type of this field in an `Option` when included in generated [`Delete`](derive@Delete), [`Insert`](derive@Insert), [`Select`](derive@Select) and [`Update`](derive@Update) structs.

---
```
# #[derive(sqly::Table)]
# #[sqly(table = "")]
# struct T {
#[sqly(default = Default::default)]
# t: i32,
# }
```
Same as above, except the default value is provided by calling the given path.

This can also be used to provide the default value for [`#[sqly(skip)]`](#skip).

<br>

#### from
---
```
# type Type = i32;
# #[derive(sqly::Table)]
# #[sqly(table = "")]
# struct T {
#[sqly(from = Type)]
# t: i32,
# }
```
Decode this field by decoding into the given type before converting with the `From<T>` implementation.

This attribute can be used along with [`#[sqly(foreign)]`](#foreign), where the given type represents the foreign table.

This will replace the type of this field when included in generated [`Delete`](derive@Delete), [`Insert`](derive@Insert), [`Select`](derive@Select) and [`Update`](derive@Update) structs.

<br>

#### skip
---
```
# #[derive(sqly::Table)]
# #[sqly(table = "")]
# struct T {
#[sqly(skip)]
# t: i32,
# }
```
Do not include this field when generating queries or structs.

When this table is included in any [`sqlx::FromRow`] or [`From<Self::Flat>`](Flat::Flat) definitions the type of this field must implement `Default`, or a custom [`#[sqly(default)]`](#default) function must be specified.

---
```
# #[derive(sqly::Table)]
# #[sqly(table = "", delete, insert, select, update)]
# struct T {
#[sqly(skip = from_row, delete, insert, select, update)]
# t: i32,
# #[sqly(key)] k: i32, v: i32,
# }
```
Same as above, except only for the operations specified.

<br>

#### key
---
```
# #[derive(sqly::Table)]
# #[sqly(table = "")]
# struct T {
#[sqly(key)]
# t: i32,
# }
```
Mark this field as a key.

Keys are used when filtering by checking for equality in the SQL `WHERE` clause.

Different operations regard this attribute differently:

[`#[derive(Delete)]`](derive@Delete) consists only of keys, therefore this attribute must not be specified.

[`#[derive(Insert)]`](derive@Insert) has no concept of keys, therefore this attribute must not be specified.

[`#[derive(Select)]`](derive@Select) consists only of keys, therefore this attribute must not be specified.

[`#[derive(Update)]`](derive@Update) uses the key fields to filter while using the other fields to set values.

[`#[derive(Table)]`](derive@Table) uses the key attribute to determine which fields to include in generated structs:

When generating [`#[sqly(delete)]`](#delete) and [`#[sqly(select)]`](#select) structs only key fields are included.

When generating [`#[sqly(update)]`](#update) structs this attribute is passed through.

When generating [`#[sqly(insert)]`](#insert) structs this attribute is ignored.

---
```
# #[derive(sqly::Table)]
# #[sqly(table = "", delete, select, update)]
# struct T {
#[sqly(key = delete, select, update)]
# t: i32,
# v: i32,
# }
```
Same as above, except only for the operations specified.

<br>
<br>