gpt4 book ai didi

python - flask -sqlalchemy。电话号码类型

转载 作者:行者123 更新时间:2023-12-01 06:35:36 25 4
gpt4 key购买 nike

models.py

class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
Name = db.Column(db.String(80), nullable=False)
# Phone = db.Column(), nullable=False) what kind of type can be here?
<小时/>

我找到了solution使用 sqlalchemy-utils 时,但是在flask-sqlalchemy 中不支持此解决方案。

from sqlalchemy_utils import PhoneNumber


class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80), nullable=False)
_phone_number = db.Column(db.Unicode(255))
phone_country_code = db.Column(db.Unicode(8))

phone_number = db.orm.composite(
PhoneNumber,
_phone_number,
phone_country_code
)

调用 Flask migrate 时出现错误:

Traceback (most recent call last):
File "/source/venv/bin/flask", line 10, in <module>
sys.exit(main())
File "/source/venv/lib/python3.7/site-packages/flask/cli.py", line 966, in main
cli.main(prog_name="python -m flask" if as_module else None)
File "/source/venv/lib/python3.7/site-packages/flask/cli.py", line 586, in main
return super(FlaskGroup, self).main(*args, **kwargs)
File "/source/venv/lib/python3.7/site-packages/click/core.py", line 717, in main
rv = self.invoke(ctx)
File "/source/venv/lib/python3.7/site-packages/click/core.py", line 1132, in invoke
cmd_name, cmd, args = self.resolve_command(ctx, args)
File "/source/venv/lib/python3.7/site-packages/click/core.py", line 1171, in resolve_command
cmd = self.get_command(ctx, cmd_name)
File "/source/venv/lib/python3.7/site-packages/flask/cli.py", line 542, in get_command
rv = info.load_app().cli.get_command(ctx, name)
File "/source/venv/lib/python3.7/site-packages/flask/cli.py", line 388, in load_app
app = locate_app(self, import_name, name)
File "/source/venv/lib/python3.7/site-packages/flask/cli.py", line 240, in locate_app
__import__(module_name)
File "/source/project.py", line 1, in <module>
from app import create_app, db
File "/source/app/__init__.py", line 30, in <module>
from app import models
File "/source/app/models.py", line 89, in <module>
class User(db.Model):
File "/source/app/models.py", line 101, in User
phone_number = db.orm.composite(
AttributeError: 'SQLAlchemy' object has no attribute 'orm'

使用flask-sqlalchemy 在数据库中存储电话号码有哪些选项?

最佳答案

根据他们的documentation ,Flask-SQLAlchemy 中的 SQLAlchemy 对象使您能够(直接)访问 sqlalchemy 和 sqlalchemy.orm 命名空间中的所有函数等,因此而不是

db.orm.composite

仅使用

db.composite

关于python - flask -sqlalchemy。电话号码类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59686863/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com