'ArgumentError 'SchemaItem' object, such as a 'Column' or a 'Constraint' expected

An error occurs while trying to create a database

Text of the error:

   Traceback (most recent call last):
      File "C:\Users\Alex\Desktop\AlNews\env\lib\site-packages\sqlalchemy\sql\schema.py", line 108, in _init_items
        spwd = item._set_parent_with_dispatch
    AttributeError: 'function' object has no attribute '_set_parent_with_dispatch'

The above exception was the direct cause of the following exception:

   Traceback (most recent call last):
      File "c:/Users/Alex/Desktop/AlNews/news_app/app.py", line 3, in <module>
        from model import db
      File "c:\Users\Alex\Desktop\AlNews\news_app\model.py", line 5, in <module>
        class News(db.Model):
      File "c:\Users\Alex\Desktop\AlNews\news_app\model.py", line 7, in News
        title = db.Column(db.text, nullable=False)
      File "C:\Users\Alex\Desktop\AlNews\env\lib\site-packages\sqlalchemy\sql\schema.py", line 1388, in __init__
        self._init_items(*args)
      File "C:\Users\Alex\Desktop\AlNews\env\lib\site-packages\sqlalchemy\sql\schema.py", line 113, in _init_items
        "'Constraint' expected, got %r" % item
      File "C:\Users\Alex\Desktop\AlNews\env\lib\site-packages\sqlalchemy\util\compat.py", line 398, in raise_from_cause
        reraise(type(exception), exception, tb=exc_tb, cause=cause)
      File "C:\Users\Alex\Desktop\AlNews\env\lib\site-packages\sqlalchemy\util\compat.py", line 152, in reraise
        raise value.with_traceback(tb)
      File "C:\Users\Alex\Desktop\AlNews\env\lib\site-packages\sqlalchemy\sql\schema.py", line 108, in _init_items
        spwd = item._set_parent_with_dispatch
    sqlalchemy.exc.ArgumentError: 'SchemaItem' object, such as a 'Column' or a 'Constraint' expected, got <function text at 0x0000000003869288>

Project strycture: project ctructure

app.py:

from flask import Flask, render_template, request

from model import db

def create_app():
    app = Flask(__name__)
    app.config.from_pyfile('config.py')
    db.init_app(app)

    @app.route('/')
    def index():
        ...

    return app

config.py:

import os

basedir = os.path.abspath(os.path.dirname(__file__))

SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, '..', 'news_app.db')

models.py:

from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()

class News(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.text, nullable=False)
    url = db.Column(db.text, unique=True, nullable=False)
    published = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
    text = db.Column(db.text, nullable=True)

    def __repr__(self):
        return '<News {} {}>'.format(self.title, self.url)

create_db.py:

from news_app.app import db, create_app
    
db.create_all(app=create_app())


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source