|
12 | 12 | from pynamodb.expressions.update import Action
|
13 | 13 | from pynamodb.exceptions import DoesNotExist, TableDoesNotExist, TableError, InvalidStateError, PutError
|
14 | 14 | from pynamodb.attributes import (
|
15 |
| - Attribute, AttributeContainer, AttributeContainerMeta, TTLAttribute, VersionAttribute |
| 15 | + AttributeContainer, AttributeContainerMeta, TTLAttribute, VersionAttribute |
16 | 16 | )
|
17 | 17 | from pynamodb.connection.table import TableConnection
|
18 | 18 | from pynamodb.expressions.condition import Condition
|
@@ -151,10 +151,6 @@ def commit(self) -> None:
|
151 | 151 | unprocessed_items = data.get(UNPROCESSED_ITEMS, {}).get(self.model.Meta.table_name)
|
152 | 152 |
|
153 | 153 |
|
154 |
| -class DefaultMeta(object): |
155 |
| - pass |
156 |
| - |
157 |
| - |
158 | 154 | class MetaModel(AttributeContainerMeta):
|
159 | 155 | table_name: str
|
160 | 156 | read_capacity_units: Optional[int]
|
@@ -184,17 +180,26 @@ def __init__(self, name: str, bases: Any, attrs: Dict[str, Any]) -> None:
|
184 | 180 | cls = cast(Type['Model'], self)
|
185 | 181 | for attr_name, attribute in cls.get_attributes().items():
|
186 | 182 | if attribute.is_hash_key:
|
| 183 | + if cls._hash_keyname and cls._hash_keyname != attr_name: |
| 184 | + raise ValueError(f"{cls.__name__} has more than one hash key: {cls._hash_keyname}, {attr_name}") |
187 | 185 | cls._hash_keyname = attr_name
|
188 | 186 | if attribute.is_range_key:
|
| 187 | + if cls._range_keyname and cls._range_keyname != attr_name: |
| 188 | + raise ValueError(f"{cls.__name__} has more than one range key: {cls._range_keyname}, {attr_name}") |
189 | 189 | cls._range_keyname = attr_name
|
190 | 190 | if isinstance(attribute, VersionAttribute):
|
191 |
| - if cls._version_attribute_name: |
| 191 | + if cls._version_attribute_name and cls._version_attribute_name != attr_name: |
192 | 192 | raise ValueError(
|
193 | 193 | "The model has more than one Version attribute: {}, {}"
|
194 | 194 | .format(cls._version_attribute_name, attr_name)
|
195 | 195 | )
|
196 | 196 | cls._version_attribute_name = attr_name
|
197 | 197 |
|
| 198 | + ttl_attr_names = [name for name, attr in cls.get_attributes().items() if isinstance(attr, TTLAttribute)] |
| 199 | + if len(ttl_attr_names) > 1: |
| 200 | + raise ValueError("{} has more than one TTL attribute: {}".format( |
| 201 | + cls.__name__, ", ".join(ttl_attr_names))) |
| 202 | + |
198 | 203 | if isinstance(attrs, dict):
|
199 | 204 | for attr_name, attr_obj in attrs.items():
|
200 | 205 | if attr_name == META_CLASS_NAME:
|
@@ -226,16 +231,6 @@ def __init__(self, name: str, bases: Any, attrs: Dict[str, Any]) -> None:
|
226 | 231 | attr_obj.Meta.model = cls
|
227 | 232 | if not hasattr(attr_obj.Meta, "index_name"):
|
228 | 233 | attr_obj.Meta.index_name = attr_name
|
229 |
| - elif isinstance(attr_obj, Attribute): |
230 |
| - if attr_obj.attr_name is None: |
231 |
| - attr_obj.attr_name = attr_name |
232 |
| - |
233 |
| - ttl_attr_names = [name for name, attr_obj in attrs.items() if isinstance(attr_obj, TTLAttribute)] |
234 |
| - if len(ttl_attr_names) > 1: |
235 |
| - raise ValueError("The model has more than one TTL attribute: {}".format(", ".join(ttl_attr_names))) |
236 |
| - |
237 |
| - if META_CLASS_NAME not in attrs: |
238 |
| - setattr(cls, META_CLASS_NAME, DefaultMeta) |
239 | 234 |
|
240 | 235 | # create a custom Model.DoesNotExist derived from pynamodb.exceptions.DoesNotExist,
|
241 | 236 | # so that "except Model.DoesNotExist:" would not catch other models' exceptions
|
|
0 commit comments