2

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?

I found a solution when using sqlalchemy-utils, however in flask-sqlalchemy this solution is not supported.

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
    )

Im getting error when calling 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'

What options are available for storing a phone number in the database by using flask-sqlalchemy?

gohxr
  • 101
  • 3
  • 10
  • Try `db.composite` (Flask-SQLA exports most of the `sqlalchemy.orm` namespace), or just use `from sqlalchemy.orm import composite` if that fails. – Ilja Everilä Jan 10 '20 at 18:11
  • @IljaEverilä will check now. Should I use PhoneNumber from sqlalchemy_utils or Flask have that type? – gohxr Jan 10 '20 at 18:14
  • From the utils. Flask-SQLA is a wrapper for SQLA that eases its integration to Flask (and provides some helpers etc.), sqla_utils is what it says on the tin. – Ilja Everilä Jan 10 '20 at 18:15
  • @Ilja Everilä if I understand you correctly ... Why then is it necessary to set sqlalchemy_utils separately? ```pip install sqlalchemy_utils``` shouldn't that be part of the Flask package list? – gohxr Jan 10 '20 at 18:17
  • No, it's a separate project. – Ilja Everilä Jan 10 '20 at 18:17
  • @IljaEverilä ```db.composite``` works well. this is the solution to my problem. I think it will be useful if you give an answer by post. I will mark as a solution to this problem. – gohxr Jan 10 '20 at 18:19

1 Answers1

1

According to their documentation, the SQLAlchemy object from Flask-SQLAlchemy gives you (direct) access to all the functions etc. from sqlalchemy and sqlalchemy.orm namespaces, so instead of

db.orm.composite

use just

db.composite
Ilja Everilä
  • 50,538
  • 7
  • 126
  • 127