@@ -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
0 commit comments