0

I'm working on a project using Python(3.7) and Flask. I'm transferring an application from Google App Engine Webapp2 in which models have been implemented using NDB, so I need to convert these models into SqlAlchemy models. I found a make_key method in models classes which I don't know what they exactly, so how can I convert it to SqlAlchemy models.

Here are some examples of models from NDB:

class Groupvalue(ndb.Model):
    """A main model for representing an individual Guestbook entry."""
    value = ndb.StringProperty()
    rank = ndb.IntegerProperty(default=1)
    username = ndb.StringProperty()

    @classmethod
    def make_key(cls, isbn):
        return ndb.Key(cls, isbn)

class LoxonValues(ndb.Model):
    """A main model for representing an individual Guestbook entry."""
    value = ndb.StringProperty()
    score = ndb.StringProperty()
    rank = ndb.IntegerProperty(default=1)
    username = ndb.StringProperty()
    group = ndb.StringProperty(default='notingroup')
    date = ndb.DateTimeProperty(auto_now_add=True)

    @classmethod
    def make_key(cls, isbn):
        return ndb.Key(cls, isbn)

and Here's how I have transferred these models into Python-SqlAlchemy:

class Groupvalue(db.Model):
    value = db.Column(db.String())
    rank = db.Column(db.Integer(default=1))
    username = db.Column(db.String())


class LoxonValues(db.Model):
    value = db.Column(db.String())
    score = db.Column(db.String())
    rank = db.Column(db.Integer(default=1))
    username = db.Column(db.String())
    group = db.Column(db.String(default='notingroup'))
    date = db.Column(db.DateTime, default=datetime.datetime.utcnow)

I'm confused about the make_key method which has been utilized into NDB models, what's the equivalent of this in SqlAlchemy?

Abdul Rehman
  • 5,326
  • 9
  • 77
  • 150

1 Answers1

0

There is no need for make_key() in sqlalchemy. Instead you mark a column as primary key.

From nbd/key.py

A key [is a]...

* a list of one or more ``(kind, id)`` pairs where ``kind`` is a string
  and ``id`` is either a string or an integer
...

The last ``(kind, id)`` pair gives the kind
and the ID of the entity that the key refers to, the others merely
specify a "parent key".

The kind is a string giving the name of the model class used to
represent the entity. In more traditional databases this would be
the table name. A model class is a Python class derived from
:class:`.Model`. Only the class name itself is used as the kind. This means
all your model classes must be uniquely named within one application. You can
override this on a per-class basis.

The ID is either a string or an integer. When the ID is a string, the
application is in control of how it assigns IDs. For example, you
could use an email address as the ID for Account entities.

...

A relational database is not hierarchical, i.e. no [(kind,id),...] key, but a primary key to address an entry in a flat table. This post has an example.

In your conversion, you seem to miss the primary and foreign key specification.

Roland Puntaier
  • 3,250
  • 30
  • 35