Giter Site home page Giter Site logo

Comments (5)

tjbanks avatar tjbanks commented on August 18, 2024

The following example also fails when trying to create the user. The only change from https://github.com/thomaxxl/safrs/blob/master/examples/mini_app.py is the biginteger primary key.

#!/usr/bin/env python
# run:
# $ FLASK_APP=mini_app flask run
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from safrs import SAFRSBase, SAFRSAPI

db = SQLAlchemy()

class User(SAFRSBase, db.Model):
    """
        description: User description
    """

    __tablename__ = "Users"
    id = db.Column(db.BigInteger(), primary_key=True)
    name = db.Column(db.String())
    email = db.Column(db.String())


def create_api(app, HOST="localhost", PORT=5000, API_PREFIX=""):
    api = SAFRSAPI(app, host=HOST, port=PORT, prefix=API_PREFIX)
    api.expose_object(User)
    user = User(name="test", email="[email protected]")
    print("Starting API: http://{}:{}/{}".format(HOST, PORT, API_PREFIX))


def create_app(config_filename=None, host="localhost"):
    app = Flask("demo_app")
    app.config.update(SQLALCHEMY_DATABASE_URI="sqlite://")
    db.init_app(app)
    with app.app_context():
        db.create_all()
        create_api(app, host)
    return app


host = "localhost"
app = create_app(host=host)

if __name__ == "__main__":
    app.run(host=host)

from safrs.

thomaxxl avatar thomaxxl commented on August 18, 2024

Thanks for reporting this, I'll look into it tomorrow.

from safrs.

thomaxxl avatar thomaxxl commented on August 18, 2024

Hi,

BigInteger doesn't have a default value (unlike db.Integer where the default value is set using the db engine autoincrement).

Does this work for you?

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from safrs import SAFRSBase, SAFRSAPI
from sqlalchemy.sql.expression import func

db = SQLAlchemy()

def my_default():
    return db.session.query(func.max(User.id)).scalar() + 1

class User(SAFRSBase, db.Model):
    """
        description: User description
    """

    __tablename__ = "Users"
    id = db.Column(db.BigInteger, primary_key=True, default=my_default)
    name = db.Column(db.String())
    email = db.Column(db.String())


def create_api(app, HOST="localhost", PORT=5000, API_PREFIX=""):
    api = SAFRSAPI(app, host=HOST, port=PORT, prefix=API_PREFIX)
    api.expose_object(User)
    user = User(name="test", email="[email protected]",id=0)
    print("Starting API: http://{}:{}/{}".format(HOST, PORT, API_PREFIX))


def create_app(config_filename=None, host="localhost"):
    app = Flask("demo_app")
    app.config.update(SQLALCHEMY_DATABASE_URI="sqlite://")
    db.init_app(app)
    with app.app_context():
        db.create_all()
        create_api(app, host)
    return app


host = "localhost"
app = create_app(host=host)

if __name__ == "__main__":
    app.run(host=host)

from safrs.

thomaxxl avatar thomaxxl commented on August 18, 2024

Btw, there's an autoincrement parameter for db.Column constructors, but the actual implementation depends on the backend, it doesn't seem to work for sqlite (but maybe it does for psql).

Also, the id can also be added in the constructor instead of using the default parameter, for example:

def my_default():
    max_id = db.session.query(func.max(User.id)).scalar()
    if max_id is None:
        return 0
    return max_id + 1

class User(SAFRSBase, db.Model):
    """
        description: User description
    """

    def __init__(self, *args, **kwargs):
        kwargs['id'] = my_default()
        super().__init__(*args, **kwargs)

    __tablename__ = "Users"
    id = db.Column(db.BigInteger, primary_key=True)
    name = db.Column(db.String())
    email = db.Column(db.String())

from safrs.

tjbanks avatar tjbanks commented on August 18, 2024

@thomaxxl

Ah, this makes complete sense and will work perfectly! Sorry this ended up being an alchemy misunderstanding on my part rather than a SAFRS issue. Thanks for your explanation and time on this fantastic project!

@tjbanks

from safrs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.