W
W
WishesFire2021-03-04 15:14:42
Flask
WishesFire, 2021-03-04 15:14:42

Can't create migration.FLASK + Postgresql + SQLAlchemy. What am I doing wrong?

app.py

app = Flask(__name__)
app.config.from_object(Config)

db = SQLAlchemy(app)
migrate = Migrate(app, db)


models.py
class User(UserMixin, db.Model):
    __tablename__ = 'user'

    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(40), unique=True, nullable=False)
    password = db.Column(db.String(50), nullable=False)
    date = db.Column(db.DateTime, default=datetime.now())

    def __init__(self, username, password):
        self.username = username
        self.password = password

    def __repr__(self):
        return f'user {self.id}'


class Profile(db.Model):
    __tablename__ = 'profile'

    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    schemas = db.Column(db.String(50))


config.py
class Config(object):
    DEBUG = True
    SECRET_KEY = str(os.getenv("SECRET_KEY"))
    SQLALCHEMY_DATABASE_URI = str(os.getenv("SQLALCHEMY_DATABASE_URI"))
    SQLALCHEMY_TRACK_MODIFICATIONS = False
SQLALCHEMY_DATABASE_URI = postgresql://postgres:[email protected]/interview (Пароль другой)


Mistake
Traceback (most recent call last):
  File "E:\python\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "E:\python\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "E:\Python3\Interview\venv\Scripts\flask.exe\__main__.py", line 7, in <module>
  File "e:\python3\interview\venv\lib\site-packages\flask\cli.py", line 967, in main
    cli.main(args=sys.argv[1:], prog_name="python -m flask" if as_module else None)
  File "e:\python3\interview\venv\lib\site-packages\flask\cli.py", line 586, in main
    return super(FlaskGroup, self).main(*args, **kwargs)
  File "e:\python3\interview\venv\lib\site-packages\click\core.py", line 782, in main
    rv = self.invoke(ctx)
  File "e:\python3\interview\venv\lib\site-packages\click\core.py", line 1259, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "e:\python3\interview\venv\lib\site-packages\click\core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "e:\python3\interview\venv\lib\site-packages\click\core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "e:\python3\interview\venv\lib\site-packages\click\decorators.py", line 73, in new_func
    return ctx.invoke(f, obj, *args, **kwargs)
  File "e:\python3\interview\venv\lib\site-packages\click\core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "e:\python3\interview\venv\lib\site-packages\flask\cli.py", line 848, in run_command
    app = DispatchingApp(info.load_app, use_eager_loading=eager_loading)
  File "e:\python3\interview\venv\lib\site-packages\flask\cli.py", line 305, in __init__
    self._load_unlocked()
  File "e:\python3\interview\venv\lib\site-packages\flask\cli.py", line 330, in _load_unlocked
    self._app = rv = self.loader()
  File "e:\python3\interview\venv\lib\site-packages\flask\cli.py", line 392, in load_app
    app = locate_app(self, import_name, None, raise_if_not_found=False)
  File "e:\python3\interview\venv\lib\site-packages\flask\cli.py", line 240, in locate_app
    __import__(module_name)
  File "E:\Python3\Interview\app.py", line 20, in <module>
    base.metadata.create_all(engine)
  File "e:\python3\interview\venv\lib\site-packages\sqlalchemy\sql\schema.py", line 4664, in create_all
    ddl.SchemaGenerator, self, checkfirst=checkfirst, tables=tables
  File "e:\python3\interview\venv\lib\site-packages\sqlalchemy\engine\base.py", line 2094, in _run_visitor
    with self._optional_conn_ctx_manager(connection) as conn:
  File "E:\python\lib\contextlib.py", line 112, in __enter__
    return next(self.gen)
  File "e:\python3\interview\venv\lib\site-packages\sqlalchemy\engine\base.py", line 2086, in _optional_conn_ctx_manager
    with self._contextual_connect() as conn:
  File "e:\python3\interview\venv\lib\site-packages\sqlalchemy\engine\base.py", line 2302, in _contextual_connect
    self._wrap_pool_connect(self.pool.connect, None),
  File "e:\python3\interview\venv\lib\site-packages\sqlalchemy\engine\base.py", line 2340, in _wrap_pool_connect
    e, dialect, self
  File "e:\python3\interview\venv\lib\site-packages\sqlalchemy\engine\base.py", line 1584, in _handle_dbapi_exception_noconnection
    sqlalchemy_exception, with_traceback=exc_info[2], from_=e
  File "e:\python3\interview\venv\lib\site-packages\sqlalchemy\util\compat.py", line 182, in raise_
    raise exception
  File "e:\python3\interview\venv\lib\site-packages\sqlalchemy\engine\base.py", line 2336, in _wrap_pool_connect
    return fn()
  File "e:\python3\interview\venv\lib\site-packages\sqlalchemy\pool\base.py", line 364, in connect
    return _ConnectionFairy._checkout(self)
  File "e:\python3\interview\venv\lib\site-packages\sqlalchemy\pool\base.py", line 778, in _checkout
    fairy = _ConnectionRecord.checkout(pool)
  File "e:\python3\interview\venv\lib\site-packages\sqlalchemy\pool\base.py", line 495, in checkout
    rec = pool._do_get()
  File "e:\python3\interview\venv\lib\site-packages\sqlalchemy\pool\impl.py", line 140, in _do_get
    self._dec_overflow()
  File "e:\python3\interview\venv\lib\site-packages\sqlalchemy\util\langhelpers.py", line 70, in __exit__
    with_traceback=exc_tb,
  File "e:\python3\interview\venv\lib\site-packages\sqlalchemy\util\compat.py", line 182, in raise_
    raise exception
  File "e:\python3\interview\venv\lib\site-packages\sqlalchemy\pool\impl.py", line 137, in _do_get
    return self._create_connection()
  File "e:\python3\interview\venv\lib\site-packages\sqlalchemy\pool\base.py", line 309, in _create_connection
    return _ConnectionRecord(self)
  File "e:\python3\interview\venv\lib\site-packages\sqlalchemy\pool\base.py", line 440, in __init__
    self.__connect(first_connect_check=True)
  File "e:\python3\interview\venv\lib\site-packages\sqlalchemy\pool\base.py", line 661, in __connect
    pool.logger.debug("Error on connect(): %s", e)
  File "e:\python3\interview\venv\lib\site-packages\sqlalchemy\util\langhelpers.py", line 70, in __exit__
    with_traceback=exc_tb,
  File "e:\python3\interview\venv\lib\site-packages\sqlalchemy\util\compat.py", line 182, in raise_
    raise exception
  File "e:\python3\interview\venv\lib\site-packages\sqlalchemy\pool\base.py", line 656, in __connect
    connection = pool._invoke_creator(self)
  File "e:\python3\interview\venv\lib\site-packages\sqlalchemy\engine\strategies.py", line 114, in connect
    return dialect.connect(*cargs, **cparams)
  File "e:\python3\interview\venv\lib\site-packages\sqlalchemy\engine\default.py", line 508, in connect
    return self.dbapi.connect(*cargs, **cparams)
  File "e:\python3\interview\venv\lib\site-packages\psycopg2\__init__.py", line 127, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError)
(Background on this error at: http://sqlalche.me/e/13/e3q8)


When creating a database through: db.create_all()
the same error

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Yakushenko, 2021-03-04
@kshnkvn

What for?

def __init__(self, username, password):
    self.username = username
    self.password = password

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question