Я пытаюсь выполнить файл sql для создания функции postgresql в моей базе данных. Но у меня есть ошибка.
File "/home/saputra/Documents/Development/Exercises/Python/flask/ujicoba06/smartapps/__init__.py", line 2, in <module>
from smartapps.app import create_app
File "/home/saputra/Documents/Development/Exercises/Python/flask/ujicoba06/smartapps/app.py", line 30, in <module>
from smartapps.commands import func_db
File "/home/saputra/Documents/Development/Exercises/Python/flask/ujicoba06/smartapps/commands/func_db.py", line 18
while current_app.open_resource("commands/func_query.sql") as f:
^
SyntaxError: invalid syntax
Моя структура:
.
|-- smartapps
| |-- app.py
| |-- commands
| | |-- func_db.py
| | |-- func_query.sql
| | |-- init_db.py
| | |-- __init__.py
func_query.sql
--function to get staff who don't have user
CREATE OR replace FUNCTION get_staff_not_have_user()
RETURNS TABLE (
real_name varchar,
email varchar
)
AS $$
BEGIN
RETURN query SELECT
st.real_name,
st.email
FROM
public.staff st
LEFT JOIN
public.users us on us.email = st.email
WHERE
us.email IS NULL;
END;
$$ LANGUAGE 'plpgsql';
func_db.py
# -*- coding: utf-8 -*-
import os
import click
from flask import current_app
from flask.cli import with_appcontext
from sqlalchemy.sql import text
from smartapps.extensions import db
def init_func():
"""Clear existing function and create new one"""
while current_app.open_resource("commands/func_query.sql") as f:
db.session.execute(f.read().decode("utf8"))
@click.command("init-func")
@with_appcontext
def func_query_command():
"""Execute sql script"""
init_func()
click.echo('Initialized function for SMART Systems.')
def init_app(app):
app.cli.add_command(func_query_command)
Когда я запускаю из терминала, я получаю ошибку.
Как исправить код для выполнения запроса из файла для создания функции в базе данных postgresql.
Спасибо