我尝试使用SQLAlchemy向Synapse数据库中插入一些行,但是在执行db.session.commit()时遇到以下错误:
ERROR - 2023-02-01 15:24:28,200 - Traceback (most recent call last):
ERROR - 2023-02-01 15:24:28,209 - File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1900, in _execute_context
ERROR - 2023-02-01 15:24:28,210 - self.dialect.do_execute(
ERROR - 2023-02-01 15:24:28,212 - File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/default.py", line 732, in do_execute
ERROR - 2023-02-01 15:24:28,213 - cursor.execute(statement, parameters)
ERROR - 2023-02-01 15:24:28,215 - pyodbc.ProgrammingError: ('42000', "[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Parse error at line: 1, column: 58: Incorrect syntax near 'OUTPUT'. (103010) (SQLExecDirectW)")
ERROR - 2023-02-01 15:24:28,216 -
ERROR - 2023-02-01 15:24:28,218 - The above exception was the direct cause of the following exception:
ERROR - 2023-02-01 15:24:28,236 - Traceback (most recent call last):
ERROR - 2023-02-01 15:24:28,246 - File "/usr/local/lib/python3.8/site-packages/flask_restx/api.py", line 672, in error_router
ERROR - 2023-02-01 15:24:28,251 - return self.handle_error(e)
ERROR - 2023-02-01 15:24:28,255 - File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 1820, in full_dispatch_request
ERROR - 2023-02-01 15:24:28,268 - rv = self.dispatch_request()
ERROR - 2023-02-01 15:24:28,280 - File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 1796, in dispatch_request
ERROR - 2023-02-01 15:24:28,285 - return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
ERROR - 2023-02-01 15:24:28,293 - File "/usr/local/lib/python3.8/site-packages/flask_restx/api.py", line 405, in wrapper
ERROR - 2023-02-01 15:24:28,306 - resp = resource(*args, **kwargs)
ERROR - 2023-02-01 15:24:28,320 - File "/usr/local/lib/python3.8/site-packages/flask/views.py", line 107, in view
ERROR - 2023-02-01 15:24:28,324 - return current_app.ensure_sync(self.dispatch_request)(**kwargs)
ERROR - 2023-02-01 15:24:28,333 - File "/usr/local/lib/python3.8/site-packages/flask_restx/resource.py", line 46, in dispatch_request
ERROR - 2023-02-01 15:24:28,339 - resp = meth(*args, **kwargs)
ERROR - 2023-02-01 15:24:28,344 - File "/code/app/main/util/decorators.py", line 53, in decorated_function
ERROR - 2023-02-01 15:24:28,347 - return f(*args, **kwargs)
ERROR - 2023-02-01 15:24:28,353 - File "/code/app/main/controller/front_controller.py", line 205, in post
ERROR - 2023-02-01 15:24:28,363 - return createUser(request)
ERROR - 2023-02-01 15:24:28,370 - File "/code/app/main/service/front_service.py", line 459, in createUser
ERROR - 2023-02-01 15:24:28,373 - db.session.commit()
ERROR - 2023-02-01 15:24:28,383 - File "<string>", line 2, in commit
ERROR - 2023-02-01 15:24:28,386 - File "/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 1451, in commit
ERROR - 2023-02-01 15:24:28,389 - self._transaction.commit(_to_root=self.future)
ERROR - 2023-02-01 15:24:28,400 - File "/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 829, in commit
ERROR - 2023-02-01 15:24:28,413 - self._prepare_impl()
ERROR - 2023-02-01 15:24:28,419 - File "/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 808, in _prepare_impl
ERROR - 2023-02-01 15:24:28,429 - self.session.flush()
ERROR - 2023-02-01 15:24:28,433 - File "/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 3383, in flush
ERROR - 2023-02-01 15:24:28,435 - self._flush(objects)
ERROR - 2023-02-01 15:24:28,438 - File "/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 3523, in _flush
ERROR - 2023-02-01 15:24:28,444 - transaction.rollback(_capture_exception=True)
ERROR - 2023-02-01 15:24:28,447 - File "/usr/local/lib/python3.8/site-packages/sqlalchemy/util/langhelpers.py", line 70, in __exit__
ERROR - 2023-02-01 15:24:28,450 - compat.raise_(
ERROR - 2023-02-01 15:24:28,466 - File "/usr/local/lib/python3.8/site-packages/sqlalchemy/util/compat.py", line 208, in raise_
ERROR - 2023-02-01 15:24:28,478 - raise exception
ERROR - 2023-02-01 15:24:28,485 - File "/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 3483, in _flush
ERROR - 2023-02-01 15:24:28,489 - flush_context.execute()
ERROR - 2023-02-01 15:24:28,506 - File "/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/unitofwork.py", line 456, in execute
ERROR - 2023-02-01 15:24:28,511 - rec.execute(self)
ERROR - 2023-02-01 15:24:28,513 - File "/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/unitofwork.py", line 630, in execute
ERROR - 2023-02-01 15:24:28,515 - util.preloaded.orm_persistence.save_obj(
ERROR - 2023-02-01 15:24:28,516 - File "/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/persistence.py", line 245, in save_obj
ERROR - 2023-02-01 15:24:28,528 - _emit_insert_statements(
ERROR - 2023-02-01 15:24:28,542 - File "/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/persistence.py", line 1238, in _emit_insert_statements
ERROR - 2023-02-01 15:24:28,548 - result = connection._execute_20(
ERROR - 2023-02-01 15:24:28,554 - File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1705, in _execute_20
ERROR - 2023-02-01 15:24:28,564 - return meth(self, args_10style, kwargs_10style, execution_options)
ERROR - 2023-02-01 15:24:28,566 - File "/usr/local/lib/python3.8/site-packages/sqlalchemy/sql/elements.py", line 333, in _execute_on_connection
ERROR - 2023-02-01 15:24:28,569 - return connection._execute_clauseelement(
ERROR - 2023-02-01 15:24:28,571 - File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1572, in _execute_clauseelement
ERROR - 2023-02-01 15:24:28,577 - ret = self._execute_context(
ERROR - 2023-02-01 15:24:28,588 - File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1943, in _execute_context
ERROR - 2023-02-01 15:24:28,600 - self._handle_dbapi_exception(
ERROR - 2023-02-01 15:24:28,610 - File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 2124, in _handle_dbapi_exception
ERROR - 2023-02-01 15:24:28,618 - util.raise_(
ERROR - 2023-02-01 15:24:28,634 - File "/usr/local/lib/python3.8/site-packages/sqlalchemy/util/compat.py", line 208, in raise_
ERROR - 2023-02-01 15:24:28,642 - raise exception
ERROR - 2023-02-01 15:24:28,647 - File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1900, in _execute_context
ERROR - 2023-02-01 15:24:28,649 - self.dialect.do_execute(
ERROR - 2023-02-01 15:24:28,652 - File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/default.py", line 732, in do_execute
ERROR - 2023-02-01 15:24:28,654 - cursor.execute(statement, parameters)
ERROR - 2023-02-01 15:24:28,668 - sqlalchemy.exc.ProgrammingError: (pyodbc.ProgrammingError) ('42000', "[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Parse error at line: 1, column: 58: Incorrect syntax near 'OUTPUT'. (103010) (SQLExecDirectW)")
ERROR - 2023-02-01 15:24:28,685 - [SQL: INSERT INTO dbo.users ([user], password, role_id, email) OUTPUT inserted.user_id VALUES (?, ?, ?, ?)]
ERROR - 2023-02-01 15:24:28,694 - [parameters: ('user', '5f4dcc3b5aa765d61d8327deb882cf99', 24, 'email')]
ERROR - 2023-02-01 15:24:28,697 - (Background on this error at: https://sqlalche.me/e/14/f405)
下面是我在Synapse中创建表时使用的SQL脚本:
CREATE TABLE [users] (
[user_id] INT NOT NULL IDENTITY(1,1) PRIMARY KEY NONCLUSTERED NOT ENFORCED,
[user] VARCHAR(25) NOT NULL,
[password] VARCHAR(32) NOT NULL,
[role_id] INT NOT NULL,
[email] VARCHAR(50) NOT NULL
);
下面是该表的SQLAlchemy模型:
class User(db.Model):
__bind_key__ = 'mssql'
__tablename__ = 'users'
user_id = db.Column(db.Integer, primary_key=True)
user = db.Column(db.String(25), nullable=False)
password = db.Column(db.String(32), nullable=False)
role_id = db.Column(db.Integer, nullable=False)
email = db.Column(db.String(50), nullable=False)
__table_args__ = (
db.PrimaryKeyConstraint('user_id', mssql_clustered=False),
{"schema": BaseModel.__table_args__["schema"]},
)
这是我用来把数据插入到de DB中的代码:
user = User(
user = data["user"],
password = hashlib.md5(data["password"].encode('utf-8')).hexdigest(),
role_id = role.role_id,
email = data["email"]
)
db.session.add(user)
db.session.commit()
有其他人遇到过这个错误吗?我真的需要一些帮助,因为我没有看到那么多关于使用Synapse的SQLAlchemy的文档。
1条答案
按热度按时间pjngdqdw1#
您的错误将模式显示为dbo
但您的create表显示该模式是test