summaryrefslogtreecommitdiff
path: root/factory/alchemy.py
diff options
context:
space:
mode:
authorrcommande <commande.romain@gmail.com>2013-06-17 23:20:57 +0200
committerRaphaƫl Barrois <raphael.barrois@polytechnique.org>2013-06-20 01:40:14 +0200
commit79ee9d24e203ad8e7daf2181437fe7132972529e (patch)
tree5fd18f176fb800024ed64129c39fae9590464db1 /factory/alchemy.py
parent5137998a856519cd11cd743d6c567497600139e8 (diff)
downloadfactory-boy-79ee9d24e203ad8e7daf2181437fe7132972529e.tar
factory-boy-79ee9d24e203ad8e7daf2181437fe7132972529e.tar.gz
Small next sequence refactoring
Diffstat (limited to 'factory/alchemy.py')
-rw-r--r--factory/alchemy.py12
1 files changed, 7 insertions, 5 deletions
diff --git a/factory/alchemy.py b/factory/alchemy.py
index 2bfaf81..ca7aefa 100644
--- a/factory/alchemy.py
+++ b/factory/alchemy.py
@@ -19,6 +19,8 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
from __future__ import unicode_literals
+from sqlalchemy.sql.functions import max
+
from . import base
@@ -30,12 +32,12 @@ class SQLAlchemyModelFactory(base.Factory):
@classmethod
def _setup_next_sequence(cls, *args, **kwargs):
"""Compute the next available PK, based on the 'pk' database field."""
- from sqlalchemy.sql.functions import max
session = cls.FACTORY_SESSION
- pk = cls.FACTORY_FOR.__table__.primary_key.columns.values()[0].key
- max_pk = session.query(max(getattr(cls.FACTORY_FOR, pk))).one()
- if isinstance(max_pk[0], int):
- return max_pk[0] + 1 if max_pk[0] else 1
+ model = cls.FACTORY_FOR
+ pk = getattr(model, model.__mapper__.primary_key[0].name)
+ max_pk = session.query(max(pk)).one()[0]
+ if isinstance(max_pk, int):
+ return max_pk + 1 if max_pk else 1
else:
return 1