Skip to content

Commit 87f59fd

Browse files
committed
Validate all model member indexes
1 parent c66bfc9 commit 87f59fd

File tree

3 files changed

+26
-21
lines changed

3 files changed

+26
-21
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 0.6.3
2+
3+
- Add workaround for subclassed pk handling
4+
15
# 0.6.2
26

37
- Add support for using multiple databases

atomdb/sql.py

+21-20
Original file line numberDiff line numberDiff line change
@@ -1299,40 +1299,41 @@ def __new__(meta, name, bases, dct):
12991299
# If a member tagged with primary_key=True is defined,
13001300
# on this class, use that as the primary key and reassign
13011301
# the _id member to alias the new primary key.
1302-
pk: Optional[Member] = None
1303-
for name, m in dct.items():
1304-
if name == "_id" or not isinstance(m, Member):
1302+
pk: Member = cls._id
1303+
for name, m in members.items():
1304+
if name == "_id":
13051305
continue
1306-
13071306
if m.metadata and m.metadata.get("primary_key"):
1308-
if pk is not None:
1307+
if pk.name != "_id" and m.name != pk.name:
13091308
raise NotImplementedError(
13101309
"Using multiple primary keys is not yet supported. "
1311-
f"Both {pk.name} and {name} are marked as primary."
1310+
f"Both {pk.name} and {m.name} are marked as primary."
13121311
)
13131312
pk = m
13141313

1315-
if pk is None:
1316-
pk = cls._id
1317-
else:
1314+
if pk is not cls._id:
1315+
# Workaround member index generation issue
1316+
# TODO: Remove this
1317+
old_index = cls._id.index
1318+
if old_index > 0 and pk.index != old_index:
1319+
pk.set_index(old_index)
1320+
13181321
# Reassign the _id field to the primary key member.
1319-
cls._id = pk
1320-
members["_id"] = pk
1322+
cls._id = members["_id"] = pk
13211323

13221324
# Remove "_id" from the fields list as it is now an alias
13231325
cls.__fields__ = tuple((f for f in cls.__fields__ if f != "_id"))
13241326

1325-
# Check that the atom member indexes are still valid after
1326-
# reassinging to avoid a bug in the past.
1327-
member_indices = set()
1328-
for name, m in members.items():
1329-
if name == "_id":
1330-
continue # The _id is an alias
1331-
assert m.index not in member_indices
1332-
member_indices.add(m.index)
1327+
# Check that the atom member indexes are still valid after
1328+
# reassinging to avoid a bug in the past.
1329+
member_indices = set()
1330+
for name, m in members.items():
1331+
if name == "_id":
1332+
continue # The _id is an alias
1333+
assert m.index not in member_indices
1334+
member_indices.add(m.index)
13331335

13341336
# Set the pk name
1335-
assert pk is not None
13361337
cls.__pk__ = (pk.metadata or {}).get("name", pk.name)
13371338

13381339
# Set to the sqlalchemy Table

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
setup(
1515
name='atom-db',
16-
version='0.6.2',
16+
version='0.6.3',
1717
author='CodeLV',
1818
author_email='[email protected]',
1919
url='https://github.com/codelv/atom-db',

0 commit comments

Comments
 (0)