【问题标题】:TypeError: Additional arguments should be named <dialectname>_<argument>, got 'nullable'类型错误:附加参数应命名为 <方言名称>_<参数>,得到“可为空”
【发布时间】:2020-12-27 23:50:18
【问题描述】:

我目前正在开发我的烧瓶应用程序,它是一个为最后一年的学生提供图书存储库的应用程序,他们可以在其中为其他学生上传他们的最后一年项目和其他相关书籍,所以我正在建立我的数据库,我遇到了这个错误信息在这里。我是编程新手,这是我的第一个项目。

v) C:\Users\USER\Desktop\educationhub>python
Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:20:19) [MSC v.1925 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from eduhub import db
C:\Users\USER\Desktop\educationhub\venv\lib\site-packages\flask_sqlalchemy\__init__.py:833: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS 
adds significant overhead and will be disabled by default in the future.  Set it to True or False to suppress this warning.
  warnings.warn(FSADeprecationWarning(
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\USER\Desktop\educationhub\eduhub.py", line 58, in <module>
    class Project(db.Model):
  File "C:\Users\USER\Desktop\educationhub\venv\lib\site-packages\flask_sqlalchemy\model.py", line 67, in __init__
    super(NameMetaMixin, cls).__init__(name, bases, d)
  File "C:\Users\USER\Desktop\educationhub\venv\lib\site-packages\flask_sqlalchemy\model.py", line 121, in __init__
    super(BindMetaMixin, cls).__init__(name, bases, d)
  File "C:\Users\USER\Desktop\educationhub\venv\lib\site-packages\sqlalchemy\ext\declarative\api.py", line 76, in __init__
    _as_declarative(cls, classname, cls.__dict__)
  File "C:\Users\USER\Desktop\educationhub\venv\lib\site-packages\sqlalchemy\ext\declarative\base.py", line 131, in _as_declarative
    _MapperConfig.setup_mapping(cls, classname, dict_)
  File "C:\Users\USER\Desktop\educationhub\venv\lib\site-packages\sqlalchemy\ext\declarative\base.py", line 160, in setup_mapping
    cfg_cls(cls_, classname, dict_)
  File "C:\Users\USER\Desktop\educationhub\venv\lib\site-packages\sqlalchemy\ext\declarative\base.py", line 190, in __init__
    self._setup_table()
  File "C:\Users\USER\Desktop\educationhub\venv\lib\site-packages\sqlalchemy\ext\declarative\base.py", line 534, in _setup_table
    cls.__table__ = table = table_cls(
  File "C:\Users\USER\Desktop\educationhub\venv\lib\site-packages\flask_sqlalchemy\model.py", line 99, in __table_cls__
    return sa.Table(*args, **kwargs)
  File "<string>", line 2, in __new__
  File "C:\Users\USER\Desktop\educationhub\venv\lib\site-packages\sqlalchemy\util\deprecations.py", line 139, in warned
    return fn(*args, **kwargs)
  File "C:\Users\USER\Desktop\educationhub\venv\lib\site-packages\sqlalchemy\sql\schema.py", line 560, in __new__
    metadata._remove_table(name, schema)
  File "C:\Users\USER\Desktop\educationhub\venv\lib\site-packages\sqlalchemy\util\langhelpers.py", line 68, in __exit__
    compat.raise_(
  File "C:\Users\USER\Desktop\educationhub\venv\lib\site-packages\sqlalchemy\util\compat.py", line 182, in raise_
    raise exception
  File "C:\Users\USER\Desktop\educationhub\venv\lib\site-packages\sqlalchemy\sql\schema.py", line 555, in __new__
    table._init(name, metadata, *args, **kw)
  File "C:\Users\USER\Desktop\educationhub\venv\lib\site-packages\sqlalchemy\sql\schema.py", line 654, in _init
    self._init_items(*args)
  File "C:\Users\USER\Desktop\educationhub\venv\lib\site-packages\sqlalchemy\sql\schema.py", line 118, in _init_items
    spwd(self)
  File "C:\Users\USER\Desktop\educationhub\venv\lib\site-packages\sqlalchemy\sql\base.py", line 464, in _set_parent_with_dispatch
    self.dispatch.after_parent_attach(self, parent)
  File "C:\Users\USER\Desktop\educationhub\venv\lib\site-packages\sqlalchemy\event\attr.py", line 322, in __call__
    fn(*args, **kw)
  File "C:\Users\USER\Desktop\educationhub\venv\lib\site-packages\sqlalchemy\sql\schema.py", line 2199, in _set_table
    self.constraint = ForeignKeyConstraint(
  File "C:\Users\USER\Desktop\educationhub\venv\lib\site-packages\sqlalchemy\sql\schema.py", line 3324, in __init__
    Constraint.__init__(
  File "C:\Users\USER\Desktop\educationhub\venv\lib\site-packages\sqlalchemy\sql\schema.py", line 2890, in __init__
    self._validate_dialect_kwargs(dialect_kw)
  File "C:\Users\USER\Desktop\educationhub\venv\lib\site-packages\sqlalchemy\sql\base.py", line 290, in _validate_dialect_kwargs
    raise TypeError(
TypeError: Additional arguments should be named <dialectname>_<argument>, got 'nullable'
>>> from eduhub import db
C:\Users\USER\Desktop\educationhub\venv\lib\site-packages\flask_sqlalchemy\__init__.py:833: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS 
adds significant overhead and will be disabled by default in the future.  Set it to True or False to suppress this warning.
  warnings.warn(FSADeprecationWarning(
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\USER\Desktop\educationhub\eduhub.py", line 63, in <module>
    class Book(db.Model):
  File "C:\Users\USER\Desktop\educationhub\venv\lib\site-packages\flask_sqlalchemy\model.py", line 67, in __init__
    super(NameMetaMixin, cls).__init__(name, bases, d)
  File "C:\Users\USER\Desktop\educationhub\venv\lib\site-packages\flask_sqlalchemy\model.py", line 121, in __init__
    super(BindMetaMixin, cls).__init__(name, bases, d)
  File "C:\Users\USER\Desktop\educationhub\venv\lib\site-packages\sqlalchemy\ext\declarative\api.py", line 76, in __init__
    _as_declarative(cls, classname, cls.__dict__)
  File "C:\Users\USER\Desktop\educationhub\venv\lib\site-packages\sqlalchemy\ext\declarative\base.py", line 131, in _as_declarative
    _MapperConfig.setup_mapping(cls, classname, dict_)
  File "C:\Users\USER\Desktop\educationhub\venv\lib\site-packages\sqlalchemy\ext\declarative\base.py", line 160, in setup_mapping
    cfg_cls(cls_, classname, dict_)
  File "C:\Users\USER\Desktop\educationhub\venv\lib\site-packages\sqlalchemy\ext\declarative\base.py", line 190, in __init__
    self._setup_table()
  File "C:\Users\USER\Desktop\educationhub\venv\lib\site-packages\sqlalchemy\ext\declarative\base.py", line 534, in _setup_table
    cls.__table__ = table = table_cls(
  File "C:\Users\USER\Desktop\educationhub\venv\lib\site-packages\flask_sqlalchemy\model.py", line 99, in __table_cls__
    return sa.Table(*args, **kwargs)
  File "<string>", line 2, in __new__
  File "C:\Users\USER\Desktop\educationhub\venv\lib\site-packages\sqlalchemy\util\deprecations.py", line 139, in warned
    return fn(*args, **kwargs)
  File "C:\Users\USER\Desktop\educationhub\venv\lib\site-packages\sqlalchemy\sql\schema.py", line 560, in __new__
    metadata._remove_table(name, schema)
  File "C:\Users\USER\Desktop\educationhub\venv\lib\site-packages\sqlalchemy\util\langhelpers.py", line 68, in __exit__
    compat.raise_(
  File "C:\Users\USER\Desktop\educationhub\venv\lib\site-packages\sqlalchemy\util\compat.py", line 182, in raise_
    raise exception
  File "C:\Users\USER\Desktop\educationhub\venv\lib\site-packages\sqlalchemy\sql\schema.py", line 555, in __new__
    table._init(name, metadata, *args, **kw)
  File "C:\Users\USER\Desktop\educationhub\venv\lib\site-packages\sqlalchemy\sql\schema.py", line 654, in _init
    self._init_items(*args)
  File "C:\Users\USER\Desktop\educationhub\venv\lib\site-packages\sqlalchemy\sql\schema.py", line 118, in _init_items
    spwd(self)
  File "C:\Users\USER\Desktop\educationhub\venv\lib\site-packages\sqlalchemy\sql\base.py", line 464, in _set_parent_with_dispatch
    self.dispatch.after_parent_attach(self, parent)
  File "C:\Users\USER\Desktop\educationhub\venv\lib\site-packages\sqlalchemy\event\attr.py", line 322, in __call__
    fn(*args, **kw)
  File "C:\Users\USER\Desktop\educationhub\venv\lib\site-packages\sqlalchemy\sql\schema.py", line 2199, in _set_table
    self.constraint = ForeignKeyConstraint(
  File "C:\Users\USER\Desktop\educationhub\venv\lib\site-packages\sqlalchemy\sql\schema.py", line 3324, in __init__
    Constraint.__init__(
  File "C:\Users\USER\Desktop\educationhub\venv\lib\site-packages\sqlalchemy\sql\schema.py", line 2890, in __init__
    self._validate_dialect_kwargs(dialect_kw)
  File "C:\Users\USER\Desktop\educationhub\venv\lib\site-packages\sqlalchemy\sql\base.py", line 290, in _validate_dialect_kwargs
    raise TypeError(
TypeError: Additional arguments should be named <dialectname>_<argument>, got 'nullable'
>>>  `

My code 

from datetime import datetime
from flask import Flask, render_template, url_for, flash, redirect
from flask_sqlalchemy import SQLAlchemy
from forms import RegistrationForm, LoginForm

app = Flask(__name__)

app.config["SECRET_KEY"] = ""
app.config["SQLALCHEMY_DATABASE_URI"] = 'sqlite:///site.db'
db = SQLAlchemy(app)




#Users Details
class Users(db.Model):
    id = db.Column(db.Integer, primary_key=True, nullable=False)
    fname = db.Column(db.String(60), nullable=False)
    lname = db.Column(db.String(60), nullable=False)
    username = db.Column(db.String(60), unique=True, nullable=False)
    email = db.Column(db.String(120), unique=True, nullable=False)
    password = db.Column(db.String(100), nullable=False)
    education = db.relationship('Education', backref='author')

    book_id = db.Column(db.Integer, db.ForeignKey('book_id'), nullable=False)
    books = db.relationship('Book', backref='author', lazy=True)

    project_id = db.Column(db.Integer, db.ForeignKey('project_id'), nullable=False)
    projects = db.relationship('Project', backref='author', lazy=True)

    #Upload Books/Projects
    uploaded_books = db.Column(db.String(120), nullable=False)
    downloaded_books = db.Column(db.String(120), nullable=False)
    uploaded_projects = db.Column(db.String(120), nullable=False)
    downloaded_books = db.Column(db.String(120), nullable=False)

    #Users Personal Details
    aboutme = db.Column(db.String(450), nullable=False, default="No Description")
    phone = db.Column(db.Numeric(20, 0), nullable=False, default="00000000000")
    location = db.Column(db.String(60), nullable=False, default="Unknown City")

    # User Image Filename
    image_file = db.Column(db.String(120), nullable=False, default="default.jpg")

    def __repr__(self):
        return f""""Users
        '{self.fname}', 
        '{self.lname}', 
        '{self.username}', 
        '{self.email}',
        '{self.uploaded_books}',
        '{self.downloaded_books}',
        '{self.uploaded_projects}',
        '{self.downloaded_projects}',
        '{self.image_file}'"""



#Educational Books
class Book(db.Model):
    id = db.Column(db.Integer, primary_key=True, nullable=False)
    book_title = db.Column(db.String(120), nullable=False)
    author = db.Column(db.String(60), nullable=False, default="Author Name")
    isdn = db.Column(db.Numeric(20, 0), unique=True, nullable=False, default="0000000000000")
    filename = db.Column(db.String(120), nullable=False, default="default.pdf")
    image = db.Column(db.String(120), nullable=False, default="default.jpg")
    date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
    book_description = db.Column(db.String(400), nullable=False, default="About Book")
    user_id = db.Column(db.Integer, db.ForeignKey('user.id', nullable=False))

    def __repr__(self):
        return f"""Book
        '{self.book_title}', 
        '{self.author}',
        '{self.isdn}',
        '{self.filename}',
        '{self.image}',
        '{self.date_posted}', 
        '{self.book_description}'"""




#Final Year Projects
class Project(db.Model):
    id = db.Column(db.Integer, primary_key=True, nullable=False)
    project_title = db.Column(db.String(60), nullable=False)
    project_author = db.Column(db.String(60),nullable=False,default="Written By")
    filename = db.Column(db.String(120), nullable=False, default="default.pdf")
    date_posted = db.Column(db.DateTime,nullable=False,default=datatime.utcnow)
    project_description = db.Column(db.String(400),nullable=False,default="About Project")
    user_id = db.Column(db.Integer, db.ForeignKey('user.id', nullable=False))

    def __repr__(self):
        return f"""Project
        '{self.project_title}', 
        '{self.project_author}', 
        '{self.filename}',
        '{self.date_posted},'
        '{self.project_description}'"""



#Edcational Qualification
class Education(db.Model):
    id = db.Column(db.Integer, primary_key=True, nullable=False)
    school = db.Column(db.String(100), nullable=True)
    institution = db.Column(db.String(120),nullable=False,default="University")
    course = db.Column(db.String(60), nullable=False, default="Economics")
    fromDate = db.Column(db.String(5), nullable=True)
    toDate = db.Column(db.Numeric(5, 0), nullable=True)
    description = db.Column(db.String(160), nullable=True)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id', nullable=False))

    def __repr__(self):
        return f"""Education
        '{self.school}', 
        '{self.institution}', 
        '{self.course}', 
        '{self.fromDate}', 
        '{self.toDate}', 
        '{self.description}'"""


@app.route("/")
@app.route("/home")
def home():
    return render_template("home.html")


@app.route("/about")
def about():
    return render_template("about.html")


@app.route("/register", methods=["GET", "POST"])
def register():
    form = RegistrationForm()
    if form.validate_on_submit():
        flash(f"Account created for { form.username.data }!" "success")
        return redirect(url_for("home"))
    return render_template("register.html", title="Register", form=form)


@app.route("/login", methods=["GET", "POST"])
def login():
    form = LoginForm()
    if form.validate_on_submit():
        if form.email.data == "admin@blog.com" and form.password.data == "password":
            flash("You have been logged in!", "success")
            return redirect(url_for("home"))
        else:
            flash("Login Unsucessful. Please Check username and password",
                  "danger")
    return render_template("login.html", title="Login", form=form)


if __name__ == "__main__":
    app.run(debug=True)

    

    

【问题讨论】:

  • 你在user_id = db.Column(db.Integer, db.ForeignKey('user.id', nullable=False)) 和其他类似的行中缺少)
  • 谢谢,我已经解决了。

标签: python-3.x database flask-sqlalchemy


【解决方案1】:

它们是来自 user_id 的拼写错误

错字

 user_id = db.Column(db.Integer, db.ForeignKey('user.id', nullable=False))

更正

 user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-16
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 2018-08-15
    • 2019-04-20
    相关资源
    最近更新 更多