OneToOneField¶
OneToOneField is the unique variant of ForeignKey.
It behaves like a foreign key at declaration and query time, but the generated relation columns are unique and the reverse accessor is singular by default.
Practical example¶
class Profile(saffier.Model):
id = saffier.IntegerField(primary_key=True, autoincrement=True)
user = saffier.OneToOneField("User", on_delete=saffier.CASCADE)
class Meta:
registry = models
Reverse behavior¶
When related_name is omitted, Saffier generates a singular reverse accessor
from the declaring model name. That means a Profile.user relation typically
becomes user.profile, not user.profiles_set.
For reverse one-to-one relations, remove() can omit the child object because
at most one related row can exist.
saffier.OneToOneField
¶
OneToOneField(to, **kwargs)
Bases: ForeignKey
Foreign-key field that enforces a unique reverse relationship.
The field is implemented as a standard foreign key with unique=True, which
gives it one-to-one semantics while reusing the full foreign-key machinery.
Source code in saffier/core/db/fields/base.py
1590 1591 1592 | |
inject_default_on_partial_update
instance-attribute
¶
inject_default_on_partial_update = get(
"inject_default_on_partial_update", False
)
force_cascade_deletion_relation
instance-attribute
¶
force_cascade_deletion_relation = (
force_cascade_deletion_relation
)
target_registry
property
writable
¶
target_registry
Return the registry used to resolve string relation targets.
Explicit target_registry overrides take precedence, followed by the
owning model registry and finally the field-level registry fallback.
target
property
¶
target
Resolve and cache the target model class for this relation.
String targets are looked up lazily so models can reference classes that are declared later in the import graph.
| RETURNS | DESCRIPTION |
|---|---|
Any
|
Resolved target model class.
TYPE:
|
related_columns
property
¶
related_columns
Return target columns used by the relation mapping.
Composite foreign keys can point to multiple target columns. When
explicit related_fields are not provided, the target model primary-key
columns are used.
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Column | None]
|
dict[str, sqlalchemy.Column | None]: Mapping of target column names to |
dict[str, Column | None]
|
SQLAlchemy columns where available. |
ForeignKeyValidator
¶
ForeignKeyValidator(
*,
title="",
description="",
help_text="",
default=NO_DEFAULT,
null=False,
read_only=False,
**kwargs,
)
Bases: SaffierField
Source code in saffier/core/db/fields/_internal.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | |
__hash__
¶
__hash__()
Source code in saffier/core/datastructures.py
19 20 21 22 23 24 25 26 | |
validate_or_error
¶
validate_or_error(value)
Source code in saffier/core/db/fields/_internal.py
71 72 73 74 75 76 | |
has_default
¶
has_default()
Source code in saffier/core/db/fields/_internal.py
78 79 80 81 82 | |
validation_error
¶
validation_error(code, value=None)
Source code in saffier/core/db/fields/_internal.py
84 85 86 | |
get_error_message
¶
get_error_message(code)
Source code in saffier/core/db/fields/_internal.py
88 89 | |
get_default_value
¶
get_default_value()
Source code in saffier/core/db/fields/_internal.py
91 92 93 94 95 | |
check
¶
check(value)
Source code in saffier/core/db/fields/base.py
711 712 713 714 715 716 717 718 | |
get_validator
¶
get_validator(**kwargs)
Source code in saffier/core/db/fields/base.py
811 812 | |
get_column_type
¶
get_column_type()
Source code in saffier/core/db/fields/base.py
139 140 | |
get_constraints
¶
get_constraints()
Source code in saffier/core/db/fields/base.py
142 143 | |
get_columns_nullable
¶
get_columns_nullable()
Resolve SQLAlchemy nullability for this field during metadata build.
The model declaration remains the canonical field definition, but migration generation can temporarily ask Saffier to render selected columns as nullable. That temporary override makes it possible to add a required field to a table that already has rows, then backfill defaults in the generated online migration before the final model constraint is enforced by later migrations.
| RETURNS | DESCRIPTION |
|---|---|
bool
|
TYPE:
|
bool
|
override should make generated columns nullable. |
Source code in saffier/core/db/fields/base.py
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | |
get_columns
¶
get_columns(name)
Source code in saffier/core/db/fields/base.py
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 | |
get_global_constraints
¶
get_global_constraints(name, columns, *, schema=None)
Build cross-table constraints and indexes for the foreign key columns.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
Logical field name on the owning model.
TYPE:
|
columns
|
Physical columns generated for the relation.
TYPE:
|
schema
|
Optional schema override for the target table reference.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Sequence[Constraint | Index]
|
typing.Sequence[sqlalchemy.Constraint | sqlalchemy.Index]: SQLAlchemy constraints and indexes that should be attached to the owning table. |
Source code in saffier/core/db/fields/base.py
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 | |
has_column
¶
has_column()
Source code in saffier/core/db/fields/base.py
179 180 | |
get_embedded_fields
¶
get_embedded_fields(field_name, existing_fields)
Source code in saffier/core/db/fields/base.py
182 183 184 185 186 187 188 | |
expand_relationship
¶
expand_relationship(value)
Normalize relation values into lightweight target model instances.
Raw primary-key values, dictionaries, proxy models, and already-loaded target instances are all accepted so callers can assign foreign keys in a natural way while the ORM preserves a consistent in-memory shape.
| PARAMETER | DESCRIPTION |
|---|---|
value
|
Incoming relation value.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Any
|
typing.Any: Target model instance or |
Source code in saffier/core/db/fields/base.py
1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 | |
clean
¶
clean(name, value, *, for_query=False)
Source code in saffier/core/db/fields/base.py
977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 | |
modify_input
¶
modify_input(name, kwargs)
Source code in saffier/core/db/fields/base.py
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 | |
raise_for_non_default
¶
raise_for_non_default(default, server_default)
Source code in saffier/core/db/fields/base.py
215 216 | |
get_default_value
¶
get_default_value()
Source code in saffier/core/db/fields/base.py
218 219 | |
has_default
¶
has_default()
Source code in saffier/core/db/fields/base.py
221 222 | |
get_default_values
¶
get_default_values(field_name, cleaned_data)
Source code in saffier/core/db/fields/base.py
224 225 226 227 228 229 230 231 | |
is_required
¶
is_required()
Source code in saffier/core/db/fields/base.py
233 234 235 236 | |
get_is_null_clause
¶
get_is_null_clause(column)
Source code in saffier/core/db/fields/base.py
238 239 | |
get_is_empty_clause
¶
get_is_empty_clause(column)
Source code in saffier/core/db/fields/base.py
241 242 | |
operator_to_clause
¶
operator_to_clause(field_name, operator, table, value)
Translate one lookup operator into a SQLAlchemy clause.
| PARAMETER | DESCRIPTION |
|---|---|
field_name
|
Logical field name being filtered.
TYPE:
|
operator
|
Saffier lookup suffix such as
TYPE:
|
table
|
SQLAlchemy table containing the target column.
TYPE:
|
value
|
Lookup value supplied by the caller.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Any
|
SQLAlchemy boolean clause implementing the lookup.
TYPE:
|
Source code in saffier/core/db/fields/base.py
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 | |
get_fk_name
¶
get_fk_name(name)
Source code in saffier/core/db/fields/base.py
847 848 849 | |
get_fkindex_name
¶
get_fkindex_name(name)
Source code in saffier/core/db/fields/base.py
851 852 853 | |
get_fk_field_name
¶
get_fk_field_name(name, fieldname)
Source code in saffier/core/db/fields/base.py
855 856 857 858 | |
get_fk_column_name
¶
get_fk_column_name(name, fieldname)
Source code in saffier/core/db/fields/base.py
860 861 862 863 864 | |
get_column_names
¶
get_column_names(name)
Source code in saffier/core/db/fields/base.py
866 867 868 869 | |
from_fk_field_name
¶
from_fk_field_name(name, fieldname)
Source code in saffier/core/db/fields/base.py
871 872 873 874 | |
pre_save_callback
async
¶
pre_save_callback(value, original_value, is_update)
Save nested related objects before persisting the owning row.
The callback allows callers to pass an unsaved related model instance or a dictionary payload. When necessary, the related object is saved first and the method returns the concrete foreign-key column values to persist on the owning model.
| PARAMETER | DESCRIPTION |
|---|---|
value
|
Current relation value being persisted.
TYPE:
|
original_value
|
Previous relation value used during updates.
TYPE:
|
is_update
|
Whether the owning model is performing an update.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Any]
|
dict[str, typing.Any]: Database-ready foreign-key column payload. |
Source code in saffier/core/db/fields/base.py
1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 | |
is_cross_db
¶
is_cross_db(owner_database=None)
Source code in saffier/core/db/fields/base.py
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 | |
get_related_model_for_admin
¶
get_related_model_for_admin()
Source code in saffier/core/db/fields/base.py
1179 1180 1181 1182 1183 1184 1185 | |
get_column
¶
get_column(name)
Source code in saffier/core/db/fields/base.py
1594 1595 | |