mrevutskyi / flask-restless-ng

A Flask extension for creating simple ReSTful JSON APIs from SQLAlchemy models.
https://flask-restless-ng.readthedocs.io
Other
64 stars 11 forks source link

Relax primary key enforcement to allow for non-primary unique keys? #45

Closed mvp closed 7 months ago

mvp commented 7 months ago

We've been using flask-restless for quite a while, and our API makes use of auto-increment numeric primary key as well as unique string, e.g:

class User(db.Model):
    __tablename__ = 'users'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.Unicode(255), nullable=False, unique=True)
    # few more fields...

This worked fine with older version. However, it last version I am getting this error:

             pk_names = primary_key_names(model)
             if primary_key:
                 if primary_key not in pk_names:
     >               raise ValueError(f'Column `{primary_key}` is not a primary key')
     E               ValueError: Column `name` is not a primary key

Is it possible to allow not just primary keys, but also unique keys as older version did?

mrevutskyi commented 7 months ago

Added allow_non_primary_key_id bool flag in version 3.2.0