I have a problem with my endpoint in Falcon framework, I have two models Class with access to DB but when execute the seconds class, this raise an exception:
sqlalchemy.exc.NoSuchColumnError: "Could not locate column in row for column 'translate'
I don't have a 'translate' column in my database or in my object mapping.
Version:
Python 3.6.2
PyMySQL==0.7.11
SQLAlchemy==1.1.11
My Code:
endpoint:
def on_post(self, req, resp, courseid, examid):
with self.db.connect() as cnn:
mdl_party = Party(cnn)
mdl_exam = Exam(cnn)
rolecourseid = mdl_party.find_rolecourseid(roleid,courseid,examid)
if rolecourseid is None :
raise AppValidationEx("ExamDoesntExist")
take_exam_id = mdl_exam.new_user_exam(username, rolecourseid, examid)
Class Party
class Party(object):
def __init__(self, dbconnection):
self.cnn = dbconnection
def find_rolecourseid(self, roleid, courseid, examid):
s = text(''' SELECT ... ''')
cursor = self.cnn.execute(s,
p_roleid=roleid,
p_courseid=courseid,
p_examid=examid)
return cursor.first()
Class Exam
class Exam(object):
""" a user class """
def __init__(self, dbconnection):
self.cnn = dbconnection
def new_user_exam(self, username, rolecourseid, examid):
now = datetime.utcnow()
data = {
'partyRoleCourseId': rolecourseid,
'examTakeDate': now,
'examTakeStart': now,
'examId': examid,
'createdBy': username,
'createdDate': now,
'deleted': 0,
'version': 0
}
ins = partyrolecourseexam.insert().values(data)
cursor = self.cnn.execute(ins) #this raise exception
take_exam_id = cursor.inserted_primary_key[0]
Database
engine = create_engine("mysql+pymysql://%s:%s#%s/%s?charset=utf8" % DBPARAMS,
pool_size=50,
max_overflow=100,
echo=False)
metadata = MetaData()
partyrolecourseexam = Table('partyrolecourseexam', metadata,
Column("id", Integer, primary_key=True),
Column("partyRoleCourseId", Integer),
...
)
The error raise when execute the "new_user_exam" method.
if the function 'find_rolecourseid' does not execute, the method "new_user_exam" works.
All trace:
Traceback (most recent call last):
File "C:\Users\USER\dev\clexam\env\lib\site-packages\sqlalchemy\engine\result.py", line 73, in __getitem__
processor, obj, index = self._keymap[key]
KeyError: 'translate'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\USER\dev\clexam\env\lib\site-packages\sqlalchemy\engine\result.py", line 99, in __getattr__
return self[name]
File "C:\Users\USER\dev\clexam\env\lib\site-packages\sqlalchemy\engine\result.py", line 75, in __getitem__
processor, obj, index = self._parent._key_fallback(key)
File "C:\Users\USER\dev\clexam\env\lib\site-packages\sqlalchemy\engine\result.py", line 563, in _key_fallback
expression._string_or_unprintable(key))
sqlalchemy.exc.NoSuchColumnError: "Could not locate column in row for column 'translate'"
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\USER\dev\clexam\env\lib\site-packages\tornado\web.py", line 1488, in _execute
result = self.prepare()
File "C:\Users\USER\dev\clexam\env\lib\site-packages\tornado\web.py", line 2774, in prepare
self.fallback(self.request)
File "C:\Users\USER\dev\clexam\env\lib\site-packages\tornado\wsgi.py", line 277, in __call__
WSGIContainer.environ(request), start_response)
File "C:\Users\USER\dev\clexam\env\lib\site-packages\falcon\api.py", line 230, in __call__
if not self._handle_exception(ex, req, resp, params):
File "C:\Users\USER\dev\clexam\env\lib\site-packages\falcon\api.py", line 657, in _handle_exception
err_handler(ex, req, resp, params)
File "app_rest.py", line 17, in handle_errors
raise ex
File "C:\Users\USER\dev\clexam\env\lib\site-packages\falcon\api.py", line 227, in __call__
responder(req, resp, **params)
File "C:\Users\USER\dev\clexam\controllers\exam_take.py", line 32, in on_post
take_exam_id = mdl_exam.new_user_exam(username, rolecourseid, examid)
File "C:\Users\USER\dev\clexam\models\exam.py", line 43, in new_user_exam
cursor = self.cnn.execute(ins)
File "C:\Users\USER\dev\clexam\env\lib\site-packages\sqlalchemy\engine\base.py", line 945, in execute
return meth(self, multiparams, params)
File "C:\Users\USER\dev\clexam\env\lib\site-packages\sqlalchemy\sql\elements.py", line 263, in _execute_on_connection
return connection._execute_clauseelement(self, multiparams, params)
File "C:\Users\USER\dev\clexam\env\lib\site-packages\sqlalchemy\engine\base.py", line 1053, in _execute_clauseelement
compiled_sql, distilled_params
File "C:\Users\USER\dev\clexam\env\lib\site-packages\sqlalchemy\engine\base.py", line 1189, in _execute_context
context)
File "C:\Users\USER\dev\clexam\env\lib\site-packages\sqlalchemy\engine\base.py", line 1405, in _handle_dbapi_exception
util.reraise(*exc_info)
File "C:\Users\USER\dev\clexam\env\lib\site-packages\sqlalchemy\util\compat.py", line 187, in reraise
raise value
File "C:\Users\USER\dev\clexam\env\lib\site-packages\sqlalchemy\engine\base.py", line 1182, in _execute_context
context)
File "C:\Users\USER\dev\clexam\env\lib\site-packages\sqlalchemy\engine\default.py", line 470, in do_execute
cursor.execute(statement, parameters)
File "C:\Users\USER\dev\clexam\env\lib\site-packages\pymysql\cursors.py", line 164, in execute
query = self.mogrify(query, args)
File "C:\Users\USER\dev\clexam\env\lib\site-packages\pymysql\cursors.py", line 143, in mogrify
query = query % self._escape_args(args, conn)
File "C:\Users\USER\dev\clexam\env\lib\site-packages\pymysql\cursors.py", line 123, in _escape_args
return dict((key, conn.literal(val)) for (key, val) in args.items())
File "C:\Users\USER\dev\clexam\env\lib\site-packages\pymysql\cursors.py", line 123, in <genexpr>
return dict((key, conn.literal(val)) for (key, val) in args.items())
File "C:\Users\USER\dev\clexam\env\lib\site-packages\pymysql\connections.py", line 821, in literal
return self.escape(obj, self.encoders)
File "C:\Users\USER\dev\clexam\env\lib\site-packages\pymysql\connections.py", line 814, in escape
return escape_item(obj, self.charset, mapping=mapping)
File "C:\Users\USER\dev\clexam\env\lib\site-packages\pymysql\converters.py", line 27, in escape_item
val = encoder(val, mapping)
File "C:\Users\USER\dev\clexam\env\lib\site-packages\pymysql\converters.py", line 110, in escape_unicode
return u"'%s'" % _escape_unicode(value)
File "C:\Users\USER\dev\clexam\env\lib\site-packages\pymysql\converters.py", line 73, in _escape_unicode
return value.translate(_escape_table)
File "C:\Users\USER\dev\clexam\env\lib\site-packages\sqlalchemy\engine\result.py", line 101, in __getattr__
raise AttributeError(e.args[0])
AttributeError: Could not locate column in row for column 'translate'
Help and thks
Related
I have model written in declarative base of SQL Alchemy.
Class Roles(Base):
__tablename__ = "roles"
__table_args__ = (
Index("roles_name", "name", unique=True),
)
id = Column(Integer, primary_key=True, default=get_uuid())
name = Column(String(10), nullable=False)
As you may have noticed I have set the default value of primary key column id to get_uuid().
def get_uuid():
pk = uuid.uuid4().int >> 64
return pk
The above method return UUID as integer of bit size 64 or less. This is because the column id of this table is set to int and spanner can hold up to 64 bit.
So now to insert a row in this table -
>>> role = Roles()
>>> role.name = "Admin"
>>> session.add(role)
>>> session.commit()
This resulted in following exception -
Traceback (most recent call last):
File "/usr/local/lib/python3.10/site-packages/google/api_core/grpc_helpers.py", line 72, in error_remapped_callable
return callable_(*args, **kwargs)
File "/usr/local/lib/python3.10/site-packages/grpc/_channel.py", line 946, in __call__
return _end_unary_response_blocking(state, call, False, None)
File "/usr/local/lib/python3.10/site-packages/grpc/_channel.py", line 849, in _end_unary_response_blocking
raise _InactiveRpcError(state)
grpc._channel._InactiveRpcError: <_InactiveRpcError of RPC that terminated with:
status = StatusCode.FAILED_PRECONDITION
details = "Could not parse 18011687921562567628 as an integer"
debug_error_string = "UNKNOWN:Error received from peer ipv4:172.19.0.3:9010 {grpc_message:"Could not parse 18011687921562567628 as an integer", grpc_status:9, created_time:"2022-11-12T06:48:36.468914625+00:00"}"
>
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/local/lib/python3.10/site-packages/google/cloud/spanner_dbapi/cursor.py", line 269, in execute
) = self.connection.run_statement(statement)
File "/usr/local/lib/python3.10/site-packages/google/cloud/spanner_dbapi/connection.py", line 454, in run_statement
_execute_insert_heterogenous(
File "/usr/local/lib/python3.10/site-packages/google/cloud/spanner_dbapi/_helpers.py", line 57, in _execute_insert_heterogenous
transaction.execute_update(
File "/usr/local/lib/python3.10/site-packages/google/cloud/spanner_v1/transaction.py", line 302, in execute_update
response = api.execute_sql(
File "/usr/local/lib/python3.10/site-packages/google/cloud/spanner_v1/services/spanner/client.py", line 1096, in execute_sql
response = rpc(
File "/usr/local/lib/python3.10/site-packages/google/api_core/gapic_v1/method.py", line 154, in __call__
return wrapped_func(*args, **kwargs)
File "/usr/local/lib/python3.10/site-packages/google/api_core/retry.py", line 283, in retry_wrapped_func
return retry_target(
File "/usr/local/lib/python3.10/site-packages/google/api_core/retry.py", line 190, in retry_target
return target()
File "/usr/local/lib/python3.10/site-packages/google/api_core/timeout.py", line 99, in func_with_timeout
return func(*args, **kwargs)
File "/usr/local/lib/python3.10/site-packages/google/api_core/grpc_helpers.py", line 74, in error_remapped_callable
raise exceptions.from_grpc_error(exc) from exc
google.api_core.exceptions.FailedPrecondition: 400 Could not parse 18011687921562567628 as an integer
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 1900, in _execute_context
self.dialect.do_execute(
File "/usr/local/lib/python3.10/site-packages/google/cloud/sqlalchemy_spanner/sqlalchemy_spanner.py", line 1013, in do_execute
cursor.execute(statement, parameters)
File "/usr/local/lib/python3.10/site-packages/google/cloud/spanner_dbapi/cursor.py", line 70, in wrapper
return function(cursor, *args, **kwargs)
File "/usr/local/lib/python3.10/site-packages/google/cloud/spanner_dbapi/cursor.py", line 289, in execute
raise IntegrityError(getattr(e, "details", e)) from e
google.cloud.spanner_dbapi.exceptions.IntegrityError: []
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/orm/session.py", line 1451, in commit
self._transaction.commit(_to_root=self.future)
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/orm/session.py", line 829, in commit
self._prepare_impl()
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/orm/session.py", line 808, in _prepare_impl
self.session.flush()
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/orm/session.py", line 3386, in flush
self._flush(objects)
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/orm/session.py", line 3525, in _flush
with util.safe_reraise():
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/util/langhelpers.py", line 70, in __exit__
compat.raise_(
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/util/compat.py", line 208, in raise_
raise exception
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/orm/session.py", line 3486, in _flush
flush_context.execute()
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/orm/unitofwork.py", line 456, in execute
rec.execute(self)
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/orm/unitofwork.py", line 630, in execute
util.preloaded.orm_persistence.save_obj(
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/orm/persistence.py", line 245, in save_obj
_emit_insert_statements(
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/orm/persistence.py", line 1238, in _emit_insert_statements
result = connection._execute_20(
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 1705, in _execute_20
return meth(self, args_10style, kwargs_10style, execution_options)
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/sql/elements.py", line 333, in _execute_on_connection
return connection._execute_clauseelement(
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 1572, in _execute_clauseelement
ret = self._execute_context(
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 1943, in _execute_context
self._handle_dbapi_exception(
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 2124, in _handle_dbapi_exception
util.raise_(
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/util/compat.py", line 208, in raise_
raise exception
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 1900, in _execute_context
self.dialect.do_execute(
File "/usr/local/lib/python3.10/site-packages/google/cloud/sqlalchemy_spanner/sqlalchemy_spanner.py", line 1013, in do_execute
cursor.execute(statement, parameters)
File "/usr/local/lib/python3.10/site-packages/google/cloud/spanner_dbapi/cursor.py", line 70, in wrapper
return function(cursor, *args, **kwargs)
File "/usr/local/lib/python3.10/site-packages/google/cloud/spanner_dbapi/cursor.py", line 289, in execute
raise IntegrityError(getattr(e, "details", e)) from e
sqlalchemy.exc.IntegrityError: (google.cloud.spanner_dbapi.exceptions.IntegrityError) []
[SQL: INSERT INTO roles (id, name) VALUES (%s, %s)]
[parameters: [18011687921562567628, 'Admin']]
(Background on this error at: https://sqlalche.me/e/14/gkpj)
What I understood for this is that the spanner is not willing to accept the generated UUID.
status = StatusCode.FAILED_PRECONDITION
details = "Could not parse 18011687921562567628 as an integer"
I have checked the method get_uuid(). It does return int value of but size 64 or less.
The README of this repo suggests creating a table's primary key as Integer and while in inserting a row in the database generate value of primary key in hex. I did exactly the same but it didn't work.
The generated int value is larger than the maximum INT64 value that is allowed in Cloud Spanner:
Max allowed: 9223372036854775807
Your value : 18011687921562567628
See https://cloud.google.com/spanner/docs/reference/standard-sql/data-types#integer_types for more information on the INT64 type.
I'm no Python expert, but my guess is that the int value that you are generating is interpreted as an unsigned int, while the INT64 data type in Cloud Spanner is signed.
EDIT: Add example to get signed value.
My understanding is that you can do the following to get a signed 64-bit integer value from a UUID in Python:
import ctypes
import uuid
ctypes.c_long(uuid.uuid4().int >> 64).value
I have this issue when I try to run upgrade on a migration that will change the type of a VARCHAR column to INTEGER and change the PK too. This is traceback:
The definition of the class is the follow:
class ActionsIntiza(Base):
__tablename__ = 'actions_intiza'
customer_user_id = sa.Column(sa.VARCHAR(20), nullable=False)
customer_id = sa.Column(sa.INTEGER(), primary_key=True, info={'sortkey': True})
intiza_action_created = sa.Column(sa.DATE(), nullable=False)
intiza_action = sa.Column(sa.VARCHAR(50), nullable=False)
intiza_action_type = sa.Column(sa.VARCHAR(50))
intiza_action_tag = sa.Column(sa.VARCHAR(50))
intiza_action_comment = sa.Column(sa.VARCHAR())
intiza_action_attachment = sa.Column(sa.VARCHAR(50))
moni_user_name = sa.Column(sa.VARCHAR(50))
The traceback:
Traceback (most recent call last):
File "C:\Users\Usuario\AppData\Roaming\Python\Python38\site-packages\sqlalchemy\sql\visitors.py", line 88, in _compiler_dispatch
meth = getter(visitor)
AttributeError: 'RedshiftDDLCompiler' object has no attribute 'visit_clause'
op.alter_column('actions_intiza', 'customer_id',
File "<string>", line 8, in alter_column
File "<string>", line 3, in alter_column
File "c:\users\usuario\appdata\local\programs\python\python38\lib\site-packages\alembic\operations\ops.py", line 1777, in alter_column
return operations.invoke(alt)
File "c:\users\usuario\appdata\local\programs\python\python38\lib\site-packages\alembic\operations\base.py", line 345, in invoke
return fn(self, operation)
File "c:\users\usuario\appdata\local\programs\python\python38\lib\site-packages\alembic\operations\toimpl.py", line 43, in alter_column
operations.impl.alter_column(
File "c:\users\usuario\appdata\local\programs\python\python38\lib\site-packages\alembic\ddl\postgresql.py", line 116, in alter_column
self._exec(
File "c:\users\usuario\appdata\local\programs\python\python38\lib\site-packages\alembic\ddl\impl.py", line 134, in _exec
return conn.execute(construct, *multiparams, **params)
File "C:\Users\Usuario\AppData\Roaming\Python\Python38\site-packages\sqlalchemy\engine\base.py", line 982, in execute
return meth(self, multiparams, params)
File "C:\Users\Usuario\AppData\Roaming\Python\Python38\site-packages\sqlalchemy\sql\ddl.py", line 72, in _execute_on_connection
return connection._execute_ddl(self, multiparams, params)
File "C:\Users\Usuario\AppData\Roaming\Python\Python38\site-packages\sqlalchemy\engine\base.py", line 1033, in _execute_ddl
compiled = ddl.compile(
File "<string>", line 1, in <lambda>
File "C:\Users\Usuario\AppData\Roaming\Python\Python38\site-packages\sqlalchemy\sql\elements.py", line 468, in compile
return self._compiler(dialect, bind=bind, **kw)
File "C:\Users\Usuario\AppData\Roaming\Python\Python38\site-packages\sqlalchemy\sql\ddl.py", line 29, in _compiler
return dialect.ddl_compiler(dialect, self, **kw)
File "C:\Users\Usuario\AppData\Roaming\Python\Python38\site-packages\sqlalchemy\sql\compiler.py", line 319, in __init__
self.string = self.process(self.statement, **compile_kwargs)
File "C:\Users\Usuario\AppData\Roaming\Python\Python38\site-packages\sqlalchemy\sql\compiler.py", line 350, in process
return obj._compiler_dispatch(self, **kwargs)
File "C:\Users\Usuario\AppData\Roaming\Python\Python38\site-packages\sqlalchemy\ext\compiler.py", line 436, in <lambda>
lambda *arg, **kw: existing(*arg, **kw),
File "C:\Users\Usuario\AppData\Roaming\Python\Python38\site-packages\sqlalchemy\ext\compiler.py", line 478, in __call__
return fn(element, compiler, **kw)
File "C:\Users\Usuario\AppData\Roaming\Python\Python38\site-packages\sqlalchemy\ext\compiler.py", line 425, in _wrap_existing_dispatch
raise exc.CompileError(
sqlalchemy.exc.CompileError: <class 'alembic.ddl.postgresql.PostgresqlColumnType'> construct has no default compilation handler.
Can you help me?
I want to synchronize one part of my One2Many field in a different model with the right match in another model. I have an one2many field in project.project which includes a field emp_id(hr.employee) and role_id(project.roles). Now in the model account.analytic.line (2nd picture) there is already the right employee so I just want to compute the right role and display it in the right field. But as far as I am now I get singleton error, I know its cause of many objects and it expects ony one. But to be honest I dont know how to compute the correct role of the right project.
This is my One2Many field (project.project)
There are many projects and i need to get the correct one with the exactly correct timesheet.
Role field (last row) account.analytic.line
My compute function:
class RoleSync(models.Model):
_inherit = 'account.analytic.line'
#_inherits = {'project.project': 'list_id'}
role_field_id = fields.Many2one(string='Role', compute='_compute_role')
def _compute_role(self):
emp = []
# to get the one2many field with the correct project
list = self.env['project.project'].search(
[('name', '=', self.project_id.name)]).list_id
# to get the right timesheet for the correct project
timesheet = self.env['account.analytic.line'].search(
[('project_id.name', '=', self.name)])
# iterate through timesheet and get name of employee
for val in timesheet:
emp.append(val.name)
# nested loop for list with employee names and the one2many field
for a in emp:
for b in list:
# if the names are the same
if a.emp == b.emp_id.name:
# take the role and display it in the field
self.role_field_id = b.emp_id.role_id
The Error:
Error:
Odoo Server Error
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/odoo/api.py", line 745, in get
value = self._data[field][record._ids[0]]
KeyError: 18
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/odoo/fields.py", line 1004, in __get__
value = env.cache.get(record, self)
File "/usr/lib/python3/dist-packages/odoo/api.py", line 751, in get
raise CacheMiss(record, field)
odoo.exceptions.CacheMiss: ('account.analytic.line(18,).role_field_id', None)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/odoo/models.py", line 5101, in ensure_one
_id, = self._ids
ValueError: too many values to unpack (expected 1)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/odoo/http.py", line 624, in _handle_exception
return super(JsonRequest, self)._handle_exception(exception)
File "/usr/lib/python3/dist-packages/odoo/http.py", line 310, in _handle_exception
raise pycompat.reraise(type(exception), exception, sys.exc_info()[2])
File "/usr/lib/python3/dist-packages/odoo/tools/pycompat.py", line 14, in reraise
raise value
File "/usr/lib/python3/dist-packages/odoo/http.py", line 669, in dispatch
result = self._call_function(**self.params)
File "/usr/lib/python3/dist-packages/odoo/http.py", line 350, in _call_function
return checked_call(self.db, *args, **kwargs)
File "/usr/lib/python3/dist-packages/odoo/service/model.py", line 94, in wrapper
return f(dbname, *args, **kwargs)
File "/usr/lib/python3/dist-packages/odoo/http.py", line 339, in checked_call
result = self.endpoint(*a, **kw)
File "/usr/lib/python3/dist-packages/odoo/http.py", line 915, in __call__
return self.method(*args, **kw)
File "/usr/lib/python3/dist-packages/odoo/http.py", line 515, in response_wrap
response = f(*args, **kw)
File "/usr/lib/python3/dist-packages/odoo/addons/web/controllers/main.py", line 1285, in search_read
return self.do_search_read(model, fields, offset, limit, domain, sort)
File "/usr/lib/python3/dist-packages/odoo/addons/web/controllers/main.py", line 1304, in do_search_read
return Model.web_search_read(domain, fields, offset=offset, limit=limit, order=sort)
File "/usr/lib/python3/dist-packages/odoo/addons/web/models/models.py", line 39, in web_search_read
records = self.search_read(domain, fields, offset=offset, limit=limit, order=order)
File "/usr/lib/python3/dist-packages/odoo/models.py", line 4951, in search_read
result = records.read(fields)
File "/usr/lib/python3/dist-packages/odoo/models.py", line 2965, in read
vals[name] = convert(record[name], record, use_name_get)
File "/usr/lib/python3/dist-packages/odoo/models.py", line 5731, in __getitem__
return self._fields[key].__get__(self, type(self))
File "/usr/lib/python3/dist-packages/odoo/fields.py", line 2333, in __get__
return super().__get__(records, owner)
File "/usr/lib/python3/dist-packages/odoo/fields.py", line 1028, in __get__
self.compute_value(recs)
File "/usr/lib/python3/dist-packages/odoo/fields.py", line 1113, in compute_value
records._compute_field_value(self)
File "/usr/lib/python3/dist-packages/odoo/models.py", line 4003, in _compute_field_value
getattr(self, field.compute)()
File "/opt/Odoo/Custom_Addon/project_addon/models/analytic_class.py", line 18, in _compute_role
[('project_id.name', '=', self.name)])
File "/usr/lib/python3/dist-packages/odoo/fields.py", line 988, in __get__
record.ensure_one()
File "/usr/lib/python3/dist-packages/odoo/models.py", line 5104, in ensure_one
raise ValueError("Expected singleton: %s" % self)
ValueError: Expected singleton: account.analytic.line(18, 12, 9, 6)
I am dealing with a huge table where I have to do query. I decided to do so by chunking my data based on user_id and every time read and write into the sql.
from sqlalchemy import create_engine
engine = create_engine('mysql+pymysql://')
q1 = "SELECT max(id) FROM users"
max_users = pd.read_sql(q1, engine)
max_users = max_users.iloc[0][0]
# since user_ids start from 1 to ... I make the split based on that
data = range(max_users)
chunks = [list(data[x:x+1000]) for x in range(0, len(data), 1000)]
def make_q(userid):
q2 = "SELECT alotofusers from bigtable WHERE userid in (" + str(','.join(str(e) for e in userid)) + ")"
from multiprocessing import Pool, TimeoutError
import time
import os
table_name = "user_type_tmp6"
def f(q):
df = pd.read_sql(q, engine)
df.to_sql(con=engine, name=table_name, if_exists='append')
pool = Pool(processes=10) # start 4 worker processes
pool.map(f, [make_q(item) for item in chunks[0:3]])
In fact my table only get populated by the first chunck but I get the following error
Exception during reset or similar
Traceback (most recent call last):
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 680, in _finalize_fairy
fairy._reset(pool)
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 867, in _reset
pool._dialect.do_rollback(self)
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/sqlalchemy/dialects/mysql/base.py", line 2302, in do_rollback
dbapi_connection.rollback()
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/pymysql/connections.py", line 430, in rollback
self._read_ok_packet()
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/pymysql/connections.py", line 394, in _read_ok_packet
pkt = self._read_packet()
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/pymysql/connections.py", line 671, in _read_packet
% (packet_number, self._next_seq_id))
pymysql.err.InternalError: Packet sequence number wrong - got 48 expected 1
multiprocessing.pool.RemoteTraceback:
"""
Traceback (most recent call last):
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1246, in _execute_context
cursor, statement, parameters, context
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/sqlalchemy/engine/default.py", line 581, in do_execute
cursor.execute(statement, parameters)
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/pymysql/cursors.py", line 170, in execute
result = self._query(query)
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/pymysql/cursors.py", line 328, in _query
conn.query(q)
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/pymysql/connections.py", line 517, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/pymysql/connections.py", line 732, in _read_query_result
result.read()
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/pymysql/connections.py", line 1075, in read
first_packet = self.connection._read_packet()
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/pymysql/connections.py", line 671, in _read_packet
% (packet_number, self._next_seq_id))
pymysql.err.InternalError: Packet sequence number wrong - got 114 expected 1
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 733, in _rollback_impl
self.engine.dialect.do_rollback(self.connection)
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/sqlalchemy/dialects/mysql/base.py", line 2302, in do_rollback
dbapi_connection.rollback()
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/pymysql/connections.py", line 429, in rollback
self._execute_command(COMMAND.COM_QUERY, "ROLLBACK")
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/pymysql/connections.py", line 750, in _execute_command
raise err.InterfaceError("(0, '')")
pymysql.err.InterfaceError: (0, '')
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/multiprocessing/pool.py", line 121, in worker
result = (True, func(*args, **kwds))
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/multiprocessing/pool.py", line 44, in mapstar
return list(map(*args))
File "user_app_usage_type.py", line 90, in f
df = pd.read_sql(q, engine) # index_col = 'user_id'
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/pandas/io/sql.py", line 436, in read_sql
chunksize=chunksize,
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/pandas/io/sql.py", line 1218, in read_query
result = self.execute(*args)
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/pandas/io/sql.py", line 1087, in execute
return self.connectable.execute(*args, **kwargs)
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 2182, in execute
return connection.execute(statement, *multiparams, **params)
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 976, in execute
return self._execute_text(object_, multiparams, params)
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1149, in _execute_text
parameters,
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1250, in _execute_context
e, statement, parameters, cursor, context
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1471, in _handle_dbapi_exception
self._autorollback()
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/sqlalchemy/util/langhelpers.py", line 79, in __exit__
compat.reraise(type_, value, traceback)
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 153, in reraise
raise value
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1471, in _handle_dbapi_exception
self._autorollback()
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 861, in _autorollback
self._root._rollback_impl()
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 735, in _rollback_impl
self._handle_dbapi_exception(e, None, None, None, None)
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1384, in _handle_dbapi_exception
exc_info,
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 398, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 152, in reraise
raise value.with_traceback(tb)
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 733, in _rollback_impl
self.engine.dialect.do_rollback(self.connection)
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/sqlalchemy/dialects/mysql/base.py", line 2302, in do_rollback
dbapi_connection.rollback()
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/pymysql/connections.py", line 429, in rollback
self._execute_command(COMMAND.COM_QUERY, "ROLLBACK")
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/pymysql/connections.py", line 750, in _execute_command
raise err.InterfaceError("(0, '')")
sqlalchemy.exc.InterfaceError: (pymysql.err.InterfaceError) (0, '')
(Background on this error at: http://sqlalche.me/e/rvf5)
"""
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "user_app_usage_type.py", line 109, in <module>
pool.map(f, [make_q(item) for item in chunks[0:3]])
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/multiprocessing/pool.py", line 268, in map
return self._map_async(func, iterable, mapstar, chunksize).get()
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/multiprocessing/pool.py", line 657, in get
raise self._value
sqlalchemy.exc.InterfaceError: (pymysql.err.InterfaceError) (0, '')
(Background on this error at: http://sqlalche.me/e/rvf5)
I am guess I am doing the multiprocessing not correct ! Or perhaps the sqlalchemy is not aligned with the pooling.
Update
From my understand by reading this and this suggested by Ilja I updated my worked function as following
def f(q):
engine = create_engine('mysql+pymysql://')
df = pd.read_sql(q, engine, index_col = 'user_id')
df.fillna(0, inplace = True)
df.to_csv('tmp.csv')
df.to_sql(con=engine, name=table_name, if_exists='append' )
engine.dispose()
but now I get errors like
multiprocessing.pool.RemoteTraceback:
"""
Traceback (most recent call last):
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1246, in _execute_context
cursor, statement, parameters, context
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/sqlalchemy/engine/default.py", line 581, in do_execute
cursor.execute(statement, parameters)
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/pymysql/cursors.py", line 170, in execute
result = self._query(query)
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/pymysql/cursors.py", line 328, in _query
conn.query(q)
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/pymysql/connections.py", line 517, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/pymysql/connections.py", line 732, in _read_query_result
result.read()
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/pymysql/connections.py", line 1075, in read
first_packet = self.connection._read_packet()
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/pymysql/connections.py", line 684, in _read_packet
packet.check_error()
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/pymysql/protocol.py", line 220, in check_error
err.raise_mysql_exception(self._data)
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/pymysql/err.py", line 109, in raise_mysql_exception
raise errorclass(errno, errval)
pymysql.err.InternalError: (1050, "Table 'users_usage_frequency_oly_12' already exists")
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/multiprocessing/pool.py", line 121, in worker
result = (True, func(*args, **kwds))
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/multiprocessing/pool.py", line 44, in mapstar
return list(map(*args))
File "user_login_type.py", line 103, in f
df.to_sql(con=engine, name=table_name, schema = 'datateam', if_exists='append' ) # dtype={'user_type': Enum('Browser', 'Hoarder', 'Mementor', 'Explorer', 'Lister', 'Scanner') }
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/pandas/core/generic.py", line 2712, in to_sql
method=method,
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/pandas/io/sql.py", line 518, in to_sql
method=method,
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/pandas/io/sql.py", line 1319, in to_sql
table.create()
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/pandas/io/sql.py", line 656, in create
self._execute_create()
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/pandas/io/sql.py", line 638, in _execute_create
self.table.create()
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/sqlalchemy/sql/schema.py", line 870, in create
bind._run_visitor(ddl.SchemaGenerator, self, checkfirst=checkfirst)
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 2049, in _run_visitor
conn._run_visitor(visitorcallable, element, **kwargs)
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1618, in _run_visitor
visitorcallable(self.dialect, self, **kwargs).traverse_single(element)
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/sqlalchemy/sql/visitors.py", line 138, in traverse_single
return meth(obj, **kw)
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/sqlalchemy/sql/ddl.py", line 826, in visit_table
include_foreign_key_constraints,
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 982, in execute
return meth(self, multiparams, params)
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/sqlalchemy/sql/ddl.py", line 72, in _execute_on_connection
return connection._execute_ddl(self, multiparams, params)
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1044, in _execute_ddl
compiled,
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1250, in _execute_context
e, statement, parameters, cursor, context
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1476, in _handle_dbapi_exception
util.raise_from_cause(sqlalchemy_exception, exc_info)
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 398, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 152, in reraise
raise value.with_traceback(tb)
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1246, in _execute_context
cursor, statement, parameters, context
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/sqlalchemy/engine/default.py", line 581, in do_execute
cursor.execute(statement, parameters)
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/pymysql/cursors.py", line 170, in execute
result = self._query(query)
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/pymysql/cursors.py", line 328, in _query
conn.query(q)
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/pymysql/connections.py", line 517, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/pymysql/connections.py", line 732, in _read_query_result
result.read()
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/pymysql/connections.py", line 1075, in read
first_packet = self.connection._read_packet()
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/pymysql/connections.py", line 684, in _read_packet
packet.check_error()
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/pymysql/protocol.py", line 220, in check_error
err.raise_mysql_exception(self._data)
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/pymysql/err.py", line 109, in raise_mysql_exception
raise errorclass(errno, errval)
sqlalchemy.exc.InternalError: (pymysql.err.InternalError) (1050, "Table 'tmp_oly_12' already exists")
[SQL:
CREATE TABLE tmp_oly_12 (
user_id BIGINT,
total_logins BIGINT,
distinct_month BIGINT,
freq TEXT,
lastlogin DATETIME,
typelastlog TEXT
)
]
(Background on this error at: http://sqlalche.me/e/2j85)
"""
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "user_login_type.py", line 125, in <module>
pool.map(f, [make_q(item) for item in chunks[0:3]])
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/multiprocessing/pool.py", line 268, in map
return self._map_async(func, iterable, mapstar, chunksize).get()
File "/Users/opt/anaconda3/envs/UserExperience/lib/python3.7/multiprocessing/pool.py", line 657, in get
raise self._value
sqlalchemy.exc.InternalError: (pymysql.err.InternalError) (1050, "Table 'users_usage_frequency_oly_12' already exists")
[SQL:
CREATE TABLE tmp_oly_12 (
user_id BIGINT,
total_logins BIGINT,
distinct_month BIGINT,
freq TEXT,
lastlogin DATETIME,
I can see the table tmp_oly_12 is populated, not fully - but still I get this error ...
I am creating a backend with multiple tables have user id as the primary field with is of UUID type in the postgresql database. But I am getting the following error:
Traceback (most recent call last):
File "/home/ayushs/.local/share/virtualenvs/Server-D_x4HQZH/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1179, in _execute_context
context = constructor(dialect, self, conn, *args)
File "/home/ayushs/.local/share/virtualenvs/Server-D_x4HQZH/lib/python3.7/site-packages/sqlalchemy/engine/default.py", line 743, in _init_compiled
for key in compiled_params
File "/home/ayushs/.local/share/virtualenvs/Server-D_x4HQZH/lib/python3.7/site-packages/sqlalchemy/engine/default.py", line 743, in <genexpr>
for key in compiled_params
File "/home/ayushs/.local/share/virtualenvs/Server-D_x4HQZH/lib/python3.7/site-packages/sqlalchemy/dialects/postgresql/psycopg2.py", line 487, in process
value = _python_UUID(value)
File "/usr/lib64/python3.7/uuid.py", line 157, in __init__
hex = hex.replace('urn:', '').replace('uuid:', '')
AttributeError: 'UUID' object has no attribute 'replace'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/ayushs/.local/share/virtualenvs/Server-D_x4HQZH/lib/python3.7/site-packages/flask/app.py", line 1832, in full_dispatch_request
rv = self.dispatch_request()
File "/home/ayushs/.local/share/virtualenvs/Server-D_x4HQZH/lib/python3.7/site-packages/flask/app.py", line 1818, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/ayushs/.local/share/virtualenvs/Server-D_x4HQZH/lib/python3.7/site-packages/flask_restful/__init__.py", line 458, in wrapper
resp = resource(*args, **kwargs)
File "/home/ayushs/.local/share/virtualenvs/Server-D_x4HQZH/lib/python3.7/site-packages/flask/views.py", line 88, in view
return self.dispatch_request(*args, **kwargs)
File "/home/ayushs/.local/share/virtualenvs/Server-D_x4HQZH/lib/python3.7/site-packages/flask_restful/__init__.py", line 573, in dispatch_request
resp = meth(*args, **kwargs)
File "/mnt/home/Programming/Projects/Social Network/Server/src/new_user.py", line 17, in post
self.database_helper(args)
File "/mnt/home/Programming/Projects/Social Network/Server/src/new_user.py", line 26, in database_helper
db.session.commit()
File "/home/ayushs/.local/share/virtualenvs/Server-D_x4HQZH/lib/python3.7/site-packages/sqlalchemy/orm/scoping.py", line 162, in do
return getattr(self.registry(), name)(*args, **kwargs)
File "/home/ayushs/.local/share/virtualenvs/Server-D_x4HQZH/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 1026, in commit
self.transaction.commit()
File "/home/ayushs/.local/share/virtualenvs/Server-D_x4HQZH/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 493, in commit
self._prepare_impl()
File "/home/ayushs/.local/share/virtualenvs/Server-D_x4HQZH/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 472, in _prepare_impl
self.session.flush()
File "/home/ayushs/.local/share/virtualenvs/Server-D_x4HQZH/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 2458, in flush
self._flush(objects)
File "/home/ayushs/.local/share/virtualenvs/Server-D_x4HQZH/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 2596, in _flush
transaction.rollback(_capture_exception=True)
File "/home/ayushs/.local/share/virtualenvs/Server-D_x4HQZH/lib/python3.7/site-packages/sqlalchemy/util/langhelpers.py", line 68, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "/home/ayushs/.local/share/virtualenvs/Server-D_x4HQZH/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 129, in reraise
raise value
File "/home/ayushs/.local/share/virtualenvs/Server-D_x4HQZH/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 2556, in _flush
flush_context.execute()
File "/home/ayushs/.local/share/virtualenvs/Server-D_x4HQZH/lib/python3.7/site-packages/sqlalchemy/orm/unitofwork.py", line 422, in execute
rec.execute(self)
File "/home/ayushs/.local/share/virtualenvs/Server-D_x4HQZH/lib/python3.7/site-packages/sqlalchemy/orm/unitofwork.py", line 589, in execute
uow,
File "/home/ayushs/.local/share/virtualenvs/Server-D_x4HQZH/lib/python3.7/site-packages/sqlalchemy/orm/persistence.py", line 245, in save_obj
insert,
File "/home/ayushs/.local/share/virtualenvs/Server-D_x4HQZH/lib/python3.7/site-packages/sqlalchemy/orm/persistence.py", line 1066, in _emit_insert_statements
c = cached_connections[connection].execute(statement, multiparams)
File "/home/ayushs/.local/share/virtualenvs/Server-D_x4HQZH/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 988, in execute
return meth(self, multiparams, params)
File "/home/ayushs/.local/share/virtualenvs/Server-D_x4HQZH/lib/python3.7/site-packages/sqlalchemy/sql/elements.py", line 287, in _execute_on_connection
return connection._execute_clauseelement(self, multiparams, params)
File "/home/ayushs/.local/share/virtualenvs/Server-D_x4HQZH/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1107, in _execute_clauseelement
distilled_params,
File "/home/ayushs/.local/share/virtualenvs/Server-D_x4HQZH/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1182, in _execute_context
e, util.text_type(statement), parameters, None, None
File "/home/ayushs/.local/share/virtualenvs/Server-D_x4HQZH/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1466, in _handle_dbapi_exception
util.raise_from_cause(sqlalchemy_exception, exc_info)
File "/home/ayushs/.local/share/virtualenvs/Server-D_x4HQZH/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 383, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "/home/ayushs/.local/share/virtualenvs/Server-D_x4HQZH/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 128, in reraise
raise value.with_traceback(tb)
File "/home/ayushs/.local/share/virtualenvs/Server-D_x4HQZH/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1179, in _execute_context
context = constructor(dialect, self, conn, *args)
File "/home/ayushs/.local/share/virtualenvs/Server-D_x4HQZH/lib/python3.7/site-packages/sqlalchemy/engine/default.py", line 743, in _init_compiled
for key in compiled_params
File "/home/ayushs/.local/share/virtualenvs/Server-D_x4HQZH/lib/python3.7/site-packages/sqlalchemy/engine/default.py", line 743, in <genexpr>
for key in compiled_params
File "/home/ayushs/.local/share/virtualenvs/Server-D_x4HQZH/lib/python3.7/site-packages/sqlalchemy/dialects/postgresql/psycopg2.py", line 487, in process
value = _python_UUID(value)
File "/usr/lib64/python3.7/uuid.py", line 157, in __init__
hex = hex.replace('urn:', '').replace('uuid:', '')
sqlalchemy.exc.StatementError: (builtins.AttributeError) 'UUID' object has no attribute 'replace'
Here is my model:
from database.shared_db import db
from sqlalchemy.dialects import postgresql
from database.shared_bycrypt import bcrypt
class AuthenticationModel(db.Model):
user_id = db.Column('UserId', postgresql.UUID, primary_key = True)
email = db.Column('Email', postgresql.TEXT, unique = False, nullable=False)
phone_no = db.Column('PhoneNo', postgresql.TEXT, unique = False, nullable=False)
password = db.Column('Password', postgresql.TEXT, unique = False, nullable=False)
def __repr__(self):
return '<User %r>' % self.user_id
#property
def password_raw(self):
raise AttributeError('password not readable')
#password_raw.setter
def password_raw(self, password):
self.password = bcrypt.generate_password_hash(password)
def verify_password(self, password):
return bcrypt.check_password_hash(self.password, password)
And here is what my database helper function:
def database_helper(self, args):
id = uuid4()
userAuth = AuthenticationModel(user_id=id, email=args['email'], phone_no=args['phone_no'], password_raw=args['password'])
userInfo = PersonalInfoModel(user_id=id, first_name=args['first_name'], last_name=args['last_name'], dob=args['dob'])
db.session.add(userAuth)
db.session.add(userInfo)
db.session.commit()
I found many examples of autogenerating UUID fields. However, if I want to create some user across multiple databases, how can I do that? Because I think I will need to be able to pass the user id to the other tables even if I autogenerate it for one of the tables. Or should I simply use a text field?
uuid4() is generating a UUID instance.
You want to pass a string for the user_id field. Convert it to a string
user_id = str(uuid4())
If you like to pass UUID instance around and get them back in the result, you should update the user_id field in your schema to:
user_id = db.Column('UserId', postgresql.UUID(as_uuid=True), primary_key=True)