Returns a table field definition that can be passed to Database.create(). The column can be indexed by setting index to True, PRIMARY or UNIQUE. Primary key number columns are always auto-incremented.
(name, type=STRING, **kwargs)
| 698 | |
| 699 | #def field(name, type=STRING, default=None, index=False, optional=True) |
| 700 | def field(name, type=STRING, **kwargs): |
| 701 | """ Returns a table field definition that can be passed to Database.create(). |
| 702 | The column can be indexed by setting index to True, PRIMARY or UNIQUE. |
| 703 | Primary key number columns are always auto-incremented. |
| 704 | """ |
| 705 | default, index, optional = ( |
| 706 | kwargs.get("default", type == DATE and NOW or None), |
| 707 | kwargs.get("index", False), |
| 708 | kwargs.get("optional", True) |
| 709 | ) |
| 710 | if type == STRING: |
| 711 | type = STRING() |
| 712 | if type == FLOAT: |
| 713 | type = "real" |
| 714 | if type == BOOLEAN: |
| 715 | type = "tinyint(1)" |
| 716 | if type == DATE: |
| 717 | type = "timestamp" |
| 718 | if str(index) in "01": |
| 719 | index = bool(int(index)) |
| 720 | if str(optional) in "01": |
| 721 | optional = bool(int(optional)) |
| 722 | return (name, type, default, index, optional) |
| 723 | |
| 724 | _field = field |
| 725 |
no test coverage detected
searching dependent graphs…