18

Everything looks fine from the documentation but it still gives me this error when I'm running the app:

  File "main.py", line 21, in <module>
    class UserSchema(ma.ModelSchema):
AttributeError: 'Marshmallow' object has no attribute 'ModelSchema'

Everything is imported correctly. The DB is committed. The behavior is the same on pipenv and venv.

Am I missing something?

from flask import Flask, jsonify
from flask_sqlalchemy import SQLAlchemy 
from flask_marshmallow import Marshmallow

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///marshmallowjson.db'

db  = SQLAlchemy(app)
ma = Marshmallow(app)

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(50))

class Item(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    item_name = db.Column(db.String(50))
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    user = db.relationship('User', backref='items')

class UserSchema(ma.ModelSchema):
    class Meta:
        model = User 
        
class ItemSchema(ma.ModelSchema):
    class Meta:
        model = Item

@app.route('/')
def index():
    users = User.query.all()
    user_schema = UserSchema(many=True)
    output = user_schema.dump(users).data
    return jsonify({'user': output})

if __name__ == '__main__':
    app.run(debug=True)
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Art Chaz
  • 421
  • 1
  • 3
  • 11

11 Answers11

33

Conf.py

from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow

db = SQLAlchemy(app)
ma = Marshmallow(app) 

# flask-marshmallow<0.12.0

class UserSchema(ma.ModelSchema):
      class Meta:
            model = User

# flask-marshmallow>=0.12.0 (recommended)

from conf import ma
class UserSchema(ma.SQLAlchemyAutoSchema):
      class Meta:
            model = User
            load_instance = True

# flask-marshmallow>=0.12.0 (not recommended)

from marshmallow_sqlalchemy import ModelSchema
class UserSchema(ModelSchema):
      class Meta:
            model = User
            sql_session = db.session
Bacar Pereira
  • 1,025
  • 13
  • 18
  • `load_instance = True` solved `sqlalchemy.orm.exc.UnmappedInstanceError: Class 'builtins.dict' is not mapped` for me. Thanks a lot. – Maxime D May 24 '20 at 16:26
  • On VS Code, using `SQLAlchemyAutoSchema` might show the following warning: "*Inheriting 'ma.SQLAlchemyAutoSchema', which is not a class.*". It's only a warning, not an error, and has no effect on the program. It can be disabled: https://stackoverflow.com/a/57222704/2745495. – Gino Mempin Jul 19 '20 at 07:25
  • `SQLAlchemyAutoSchema` has a different `load` method to `ModelSchema` in ways I cannot get my head round. Doesn't return an object and an error any more? e.g. `puppy, errors = puppy_schema.load(request.form)` now chokes – Jack Deeth Mar 12 '21 at 23:19
24

I hate when it happens, but i got the answer immediately after posting...

Was installed only flask-marshmallow, but

pipenv install marshmallow-sqlalchemy

needed to add to work with SQLAlchemy. Whole code stays the same.

Maybe it will help someone... Now i got a different issue but that's another story.

Art Chaz
  • 421
  • 1
  • 3
  • 11
  • I tried doing this but did not work, same error keeps popping up – GKV Apr 27 '20 at 06:49
  • 3
    @GKV see https://stackoverflow.com/a/61500954/1157536 . ModelSchema was removed from flask-marshmallow. You should migrate to SQLAlchemySchema instead: https://flask-marshmallow.readthedocs.io/en/latest/changelog.html#changelog – Steve L Apr 29 '20 at 19:31
  • `from marshmallow_sqlalchemy import SQLAlchemyAutoSchema` – Jean Monet Jun 14 '21 at 14:33
7

Cool ! it got deprecated in the newer version .

class UserSchema(ma.ModelSchema):
class Meta:
    model = User 

change that to

class UserSchema(ma.SQLAlchemyAutoSchema):
class Meta:
    model=User
    load_instance=True

this should work well now!!

Pavan elisetty
  • 182
  • 1
  • 9
4

I had marshmallow-sqlalchemy installed, But i still get 'Marshmallow' object has no attribute 'ModelSchema'. For me the following solved the problem.

from marshmallow_sqlalchemy import ModelSchema

class UserSchema(ModelSchema):
    class Meta:
        model = User
smr
  • 49
  • 1
4

From https://github.com/marshmallow-code/flask-marshmallow/issues/56:

See https://github.com/marshmallow-code/flask-marshmallow/blob/dev/CHANGELOG.rst#0120-2020-04-26 . It is recommended that you use the newer SQLAlchemySchema rather than ModelSchema.

Breaking change: ma.ModelSchema and ma.TableSchema are removed, since these are deprecated upstream.

Ri1a
  • 737
  • 9
  • 26
3

Generate marshmallow Schemas from your models using SQLAlchemySchema or SQLAlchemyAutoSchema. https://flask-marshmallow.readthedocs.io/en/latest/

from flask_marshmallow import Marshmallow
from .models import Author

ma = Marshmallow()

class AuthorSchema(ma.SQLAlchemySchema):
    class Meta:
        model = Author

    id = ma.auto_field()
    name = ma.auto_field()
    books = ma.auto_field()
Luis Valverde
  • 431
  • 4
  • 5
2

When a new version was released, it showed this error. Breaking change: ma.ModelSchema and ma.TableSchema are removed, since these are deprecated upstream.

Please uninstall your current packages and install them with this version.

Click==7.0
Flask==1.1.1
Flask-Cors==3.0.8
Flask-Login==0.5.0
flask-marshmallow==0.9.0
Flask-SQLAlchemy==2.4.1
Flask-WTF==0.14.3
itsdangerous==1.1.0
Jinja2==2.11.1
MarkupSafe==1.1.1
marshmallow==2.20.5
marshmallow-sqlalchemy==0.18.0
six==1.14.0
SQLAlchemy==1.3.13
Werkzeug==1.0.0
WTForms==2.2.1

Good luck

ExpertWeblancer
  • 1,368
  • 1
  • 13
  • 28
1

on 2020. it changed to Schema instead of ModelSchema

check official release note https://flask-marshmallow.readthedocs.io/en/latest/

KeepLearning
  • 517
  • 7
  • 10
1

Keep good practice in reading the documentation. The solution for you can be this:

class UserSchema(ma.ModelSchema):
    class Meta:
        model = User 

In my case I have version 14. So over time, they may change. For this reason, see the documentation: https://flask-marshmallow.readthedocs.io/en/latest/

0

I struggled with this for 2 days. Nothing worked, at the end I chagned the version of marshmallow-sqlalchemy from 0.23.0 to 0.22.3

pip uninstall marshmallow-sqlalchemy
pip install marshmallow-sqlalchemy==0.22.3

and the error disappeared. I hope no one else have to waste their time like I did.

GKV
  • 864
  • 1
  • 12
  • 25
0

With the release of Flask-Marshmallow 0.12.0 (2020-04-26), the ma.ModelSchema got depreceated. You should be switching to ma.SQLAlchemySchema or ma.SQLAlchemyAutoSchema.

See more at https://flask-marshmallow.readthedocs.io/en/latest/changelog.html#id2

Zoltan Fedor
  • 2,004
  • 2
  • 23
  • 40