问题
I'm trying to connect google cloud function with google cloud sql (postgreSQL).
I found document and do exactly same things with document (I think...) but I got error.
https://cloud.google.com/sql/docs/postgres/quickstart-connect-functions
my cloud function requirements.txt
SQLAlchemy==1.3.12
pg8000==1.13.2
and my main.py
import sqlalchemy
# Set the following variables depending on your specific
# connection name and root password from the earlier steps:
connection_name = "tram-2020:us-central1:tram"
db_password = "[my password]"
db_name = "guestbook"
db_user = "postgres"
driver_name = 'postgres+pg8000'
query_string = dict({"unix_sock": "/cloudsql/{}/.s.PGSQL.5432".format(connection_name)})
def insert(request):
request_json = request.get_json()
stmt = sqlalchemy.text('INSERT INTO entries (guestName, content) values ("third guest", "Also this one");')
db = sqlalchemy.create_engine(
sqlalchemy.engine.url.URL(
drivername=driver_name,
username=db_user,
password=db_password,
database=db_name,
query=query_string,
),
pool_size=5,
max_overflow=2,
pool_timeout=30,
pool_recycle=1800
)
try:
with db.connect() as conn:
conn.execute(stmt)
except Exception as e:
return 'Error: {}'.format(str(e))
return 'ok'
And I got error like this
Error: (pg8000.core.ProgrammingError) {'S': 'ERROR', 'V': 'ERROR', 'C': '42703', 'M': 'column "third guest" does not exist', 'P': '50', 'F': 'parse_relation.c', 'L': '3359', 'R': 'errorMissingColumn'}
[SQL: INSERT INTO entries (guestName, content) values ("third guest", "Also this one");]
(Background on this error at: http://sqlalche.me/e/f405)
Why error said MissingColumn, did I use wrong query? Please help me.. How can I solve this problem..
回答1:
I followed the tutorial and faced the same issue.
I had to change the line of code:
stmt = sqlalchemy.text('INSERT INTO entries (guestName, content) values ("third guest", "Also this one");')
to
stmt = sqlalchemy.text("INSERT INTO entries (guestName, content) values ('third guest', 'Also this one');")
Everything worked as expected after that.
来源:https://stackoverflow.com/questions/64299003/connecting-google-cloud-function-to-cloud-sql-postgresql