flask-sqlalchemy 定义一对多关系报错:NameError: name ‘ArticleDate’ is not defined

flask-sqlalchemy

#1

创建数据库时 ,报错如下:
$ flask initdb
Traceback (most recent call last):
File “c:\users\13935\desktop\blog\venv\lib\site-packages\sqlalchemy\ext\declarative\clsregistry.py”, line 294, in call
x = eval(self.arg, globals(), self._dict)
File “”, line 1, in
NameError: name ‘ArticleDate’ is not defined

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “C:\Users\13935\Anaconda3\lib\runpy.py”, line 193, in _run_module_as_main
main”, mod_spec)
File “C:\Users\13935\Anaconda3\lib\runpy.py”, line 85, in run_code
exec(code, run_globals)
File "C:\Users\13935\Desktop\blog\venv\Scripts\flask.exe_main
.py", line 9, in
File “c:\users\13935\desktop\blog\venv\lib\site-packages\flask\cli.py”, line 894, in main
cli.main(args=args, prog_name=name)
File “c:\users\13935\desktop\blog\venv\lib\site-packages\flask\cli.py”, line 557, in main
return super(FlaskGroup, self).main(*args, **kwargs)
File “c:\users\13935\desktop\blog\venv\lib\site-packages\click\core.py”, line 717, in main
rv = self.invoke(ctx)
File “c:\users\13935\desktop\blog\venv\lib\site-packages\click\core.py”, line 1137, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File “c:\users\13935\desktop\blog\venv\lib\site-packages\click\core.py”, line 956, in invoke
return ctx.invoke(self.callback, **ctx.params)
File “c:\users\13935\desktop\blog\venv\lib\site-packages\click\core.py”, line 555, in invoke
return callback(*args, **kwargs)
File “c:\users\13935\desktop\blog\venv\lib\site-packages\click\decorators.py”, line 17, in new_func
return f(get_current_context(), *args, **kwargs)
File “c:\users\13935\desktop\blog\venv\lib\site-packages\flask\cli.py”, line 412, in decorator
return _ctx.invoke(f, *args, **kwargs)
File “c:\users\13935\desktop\blog\venv\lib\site-packages\click\core.py”, line 555, in invoke
return callback(*args, **kwargs)
File "C:\Users\13935\Desktop\blog\app_init
.py", line 39, in initdb
admin = Admin(username=‘admin’)
File “”, line 2, in init
File “c:\users\13935\desktop\blog\venv\lib\site-packages\sqlalchemy\orm\instrumentation.py”, line 373, in _new_state_if_none
state = self._state_constructor(instance, self)
File “c:\users\13935\desktop\blog\venv\lib\site-packages\sqlalchemy\util\langhelpers.py”, line 855, in get
obj.dict[self.name] = result = self.fget(obj)
File “c:\users\13935\desktop\blog\venv\lib\site-packages\sqlalchemy\orm\instrumentation.py”, line 199, in state_constructor
self.dispatch.first_init(self, self.class
)
File “c:\users\13935\desktop\blog\venv\lib\site-packages\sqlalchemy\event\attr.py”, line 297, in call
fn(*args, **kw)
File “c:\users\13935\desktop\blog\venv\lib\site-packages\sqlalchemy\orm\mapper.py”, line 3341, in _event_on_first_init
configure_mappers()
File “c:\users\13935\desktop\blog\venv\lib\site-packages\sqlalchemy\orm\mapper.py”, line 3229, in configure_mappers
mapper._post_configure_properties()
File “c:\users\13935\desktop\blog\venv\lib\site-packages\sqlalchemy\orm\mapper.py”, line 1947, in _post_configure_properties
prop.init()
File “c:\users\13935\desktop\blog\venv\lib\site-packages\sqlalchemy\orm\interfaces.py”, line 196, in init
self.do_init()
File “c:\users\13935\desktop\blog\venv\lib\site-packages\sqlalchemy\orm\relationships.py”, line 1853, in do_init
self._process_dependent_arguments()
File “c:\users\13935\desktop\blog\venv\lib\site-packages\sqlalchemy\orm\relationships.py”, line 1915, in _process_dependent_arguments
self.target = self.entity.persist_selectable
File “c:\users\13935\desktop\blog\venv\lib\site-packages\sqlalchemy\util\langhelpers.py”, line 855, in get
obj.dict[self.name] = result = self.fget(obj)
File “c:\users\13935\desktop\blog\venv\lib\site-packages\sqlalchemy\orm\relationships.py”, line 1820, in entity
argument = self.argument()
File “c:\users\13935\desktop\blog\venv\lib\site-packages\sqlalchemy\ext\declarative\clsregistry.py”, line 306, in call
% (self.prop.parent, self.arg, n.args[0], self.cls)
sqlalchemy.exc.InvalidRequestError: When initializing mapper mapped class Category->category, expression ‘ArticleDate’ failed to locate a name (“name ‘ArticleDate’ is not defined”). If this is a class name, consider adding this relationship() to the <class ‘app.models.Category’> class after both dependent classes have been defined.

创建数据库 flask init代码如下:
def register_commands(app):
@app.cli.command()
def initdb():
db.create_all()
admin = Admin(username=‘admin’)
admin.set_password_hash(‘12345’)
db.session.add(admin)
db.session.commit()
click.echo(‘data successs’)

models 定义表代码如下:
class Admin(db.Model, UserMixin):
tablename = ‘admin’
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(20))
password_hash = db.Column(db.String(128))
blog_title = db.Column(db.String(10), default=‘BLOG’)

def set_password_hash(self, password):
self.password_hash = generate_password_hash(password)

def validate_password(self, password):
return check_password_hash(self.password_hash, password)

class ArticleData(db.Model):
tablename = ‘articledate’
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(50))
body = db.Column(db.Text)
timestamp = db.Column(db.DateTime, default=datetime.now, index=True)
read_number = db.Column(db.Integer, default=0)
category_id = db.Column(db.Integer, db.ForeignKey(‘category.id’))

class Category(db.Model):
tablename = ‘category’
id = db.Column(db.Integer, primary_key=True)
category_name = db.Column(db.String(10), unique=True)
articles = db.relationship(‘ArticleDate’)

错误描述:如果不定义ArticleDate表 和 Category表 一对多关系。则能成功创建数据库。定义关系,创建数据库则报错。


#2

articles = db.relationship(‘ArticleDate’)这一句转换为ArticleData类的category= relationship(...)试试。

顺便学习下如何使用代码块:

请为代码块设置语法高亮,使用编辑器上面的按钮,或是参考论坛说明里的编辑器使用一节


#3

ArticleDate错了,是ArticleData


#4

粗心了,非常感谢。问题已解决


#5

哈哈哈我说一个笑话:

有一次项目组发现getDate()有bug,找了几天了都没找到。后来深入排查,原来是某程序员本来想写getData()的哈哈哈哈。:rofl::rofl::rofl::rofl::rofl: