ArgumentError“SchemaItem”对象,例如预期的“列”或“约束”

Ale*_*777 5 python sqlalchemy python-3.x flask-sqlalchemy

尝试创建数据库时发生错误

错误文本:


   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'
Run Code Online (Sandbox Code Playgroud)

上述异常是导致以下异常的直接原因:

   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>

Run Code Online (Sandbox Code Playgroud)

模型.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)
Run Code Online (Sandbox Code Playgroud)

小智 0

当指定的列的数据类型存在冲突时,就会出现此问题。就您而言,冲突在于db.text您将其作为 class 列的类型传递News。请检查数据库文件中的数据类型名称和类名称在任何时间点是否相同。他们不应该这样。