1

AttributeError: sqlalchemy object has no attribute "Models" error is occurring. I already installed all requirements like pip install sqlalchemy, flask-sqlalchemy, psycopy2-binary. this file name is create.py

import os

from flask import Flask, render_template, request
from models import *

app = Flask(__name__)

app.config["SQLALchemy_DATABASE_URI"] = os.getenv("DATABASE_URL")
app.config["SQLALchemy_TRACK_MODIFICATIONS"] = False
db.init_app(app)

def main():
   db.create_all()
if __name__ == "__main__":
    with app.app_context():
        main()

this file is models.py

 from flask_sqlalchemy import SQLAlchemy

db=SQLAlchemy()

class Flight(db.Models):
    __tablename__ = "flights"
    id = db.Column(db.Integer, primary_key=True)
    origin = db.Column(db.String, nullable=False)
    destination = db.Column(db.String, nullable=False)
    duration = db.Column(db.Integer, nullable=False)

class Passenger(db.Models):
    __tablename__ = "passengers"
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String, nullable=False)
    flight_id = db.Column(db.Integer, db.ForeignKey("flights.id", 
nullable=False))

output is:

Traceback (most recent call last): File "create.py", line 4, in from models import * File "D:\web\flaskrun\models.py", line 5, in class Flight(db.Models): AttributeError: 'SQLAlchemy' object has no attribute 'Models'

1

3 Answers 3

4

As mentioned in the flask-sqlalchemy docs your models need to derive from db.Model without the trailing s

Furthermore it provides a class called Model that is a declarative base which can be used to declare models:

Sign up to request clarification or add additional context in comments.

2 Comments

File "C:\Users\areb fg\Anaconda3\lib\site-packages\sqlalchemy\sql\schema.py", line 2711, in init self._validate_dialect_kwargs(dialect_kw) File "C:\Users\areb fg\Anaconda3\lib\site-packages\sqlalchemy\sql\base.py", line 289, in _validate_dialect_kwargs "named <dialectname>_<argument>, got '%s'" % k TypeError: Additional arguments should be named <dialectname>_<argument>, got 'nullable'
as it appears there are some other issues in the code, check the entire traceback (in the snippet you sent I don't see anymore issues with db.Model)
3

It should be db.Model instead of db.Models.

Comments

1

I got this same error because there was typing mistake I made the s small in string method for datatype like

db.String()--->correct

db.string()--->Incorrect

name=db.Column(db.String(),nullable=False)

Hope this helps someone

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.