SQLAlchemy try to insert null value on primary key - python

I have to work on an already written code using SQLAlchemy, Marshmellow and Flask and im trying to add new users to a database. I'm still studying the code trying to figure out if it works or not, but anytime that i try to add a new entry to the db it calls me an exception
sqlalchemy.exc.IntegrityError: (psycopg2.IntegrityError) null value in column "id" violates not-null constraint
This is my SQLAlchemy model user.py
from .. import db
class User(db.Model):
__tablename__ = 'user'
id = db.Column(db.Integer, primary_key=True)
age = db.Column(db.Integer)
gender = db.Column(db.String)
region = db.Column(db.String)
date_created = db.Column(db.DateTime, default=db.func.current_timestamp())
def __init__(self, age, gender, region):
self.age = age
self.gender = gender
self.region = region
def __repr__(self):
return '<User {1}>'.format(self.id)
and this is the api that manage the POST operation
#api.route('/users', methods=['POST'])
def create_user():
print("debug")
json_data = request.get_json()
print(json_data)
if not json_data:
return jsonify({'message': 'Invalid Request'}), 400
# Validate and deserialize input
user,errors = user_schema.load(json_data)
if errors:
return jsonify(errors), 422
# save new user to db
new_user = User(user.age,user.gender,user.region)
print(user.age)
db.session.add(new_user)
if db.session:
db.session.commit()
initialize(0, user)
print("wtf")
return jsonify({"message": "Created new user.",
"user": json_data}), 201
and it always has problems with the commit
How can i resolve it ? Im losing my mind working on i
EDIT:
It seems that the db is connected because i can do db.session.add(user) without problems
This is the full traceback
Traceback (most recent call last):
File "/home/leonardo/.local/lib/python3.5/site-packages/flask/app.py",
line 2000, in __call__
return self.wsgi_app(environ, start_response)
File "/home/leonardo/.local/lib/python3.5/site-packages/flask/app.py",
line 1991, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/home/leonardo/.local/lib/python3.5/site-packages/flask/app.py",
line 1567, in handle_exception
reraise(exc_type, exc_value, tb)
File "/home/leonardo/.local/lib/python3.5/site-
packages/flask/_compat.py", line 33, in reraise
raise value
File "/home/leonardo/.local/lib/python3.5/site-packages/flask/app.py",
line 1988, in wsgi_app
response = self.full_dispatch_request()
File "/home/leonardo/.local/lib/python3.5/site-packages/flask/app.py",
line 1641, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/leonardo/.local/lib/python3.5/site-packages/flask/app.py",
line 1544, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/leonardo/.local/lib/python3.5/site-
packages/flask/_compat.py", line 33, in reraise
raise value
File "/home/leonardo/.local/lib/python3.5/site-packages/flask/app.py",
line 1639, in full_dispatch_request
rv = self.dispatch_request()
File "/home/leonardo/.local/lib/python3.5/site-packages/flask/app.py",
line 1625, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/leonardo/Università/Tirocinio/tim-recommender-
api/app/api/user.py", line 44, in create_user
db.session.commit()
File "/home/leonardo/.local/lib/python3.5/site-
packages/sqlalchemy/orm/scoping.py", line 157, in do
return getattr(self.registry(), name)(*args, **kwargs)
File "/home/leonardo/.local/lib/python3.5/site-
packages/sqlalchemy/orm/session.py", line 801, in commit
self.transaction.commit()
File "/home/leonardo/.local/lib/python3.5/site-
packages/sqlalchemy/orm/session.py", line 392, in commit
self._prepare_impl()
File "/home/leonardo/.local/lib/python3.5/site-
packages/sqlalchemy/orm/session.py", line 372, in _prepare_impl
self.session.flush()
File "/home/leonardo/.local/lib/python3.5/site-
packages/sqlalchemy/orm/session.py", line 2019, in flush
self._flush(objects)
File "/home/leonardo/.local/lib/python3.5/site-
packages/sqlalchemy/orm/session.py", line 2137, in _flush
transaction.rollback(_capture_exception=True)
File "/home/leonardo/.local/lib/python3.5/site-
packages/sqlalchemy/util/langhelpers.py", line 60, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "/home/leonardo/.local/lib/python3.5/site-
packages/sqlalchemy/util/compat.py", line 186, in reraise
raise value
File "/home/leonardo/.local/lib/python3.5/site-
packages/sqlalchemy/orm/session.py", line 2101, in _flush
flush_context.execute()
File "/home/leonardo/.local/lib/python3.5/site-
packages/sqlalchemy/orm/unitofwork.py", line 373, in execute
rec.execute(self)
File "/home/leonardo/.local/lib/python3.5/site-
packages/sqlalchemy/orm/unitofwork.py", line 532, in execute
uow
File "/home/leonardo/.local/lib/python3.5/site-
packages/sqlalchemy/orm/persistence.py", line 174, in save_obj
mapper, table, insert)
File "/home/leonardo/.local/lib/python3.5/site-
packages/sqlalchemy/orm/persistence.py", line 800, in
_emit_insert_statements
execute(statement, params)
File "/home/leonardo/.local/lib/python3.5/site-
packages/sqlalchemy/engine/base.py", line 914, in execute
return meth(self, multiparams, params)
File "/home/leonardo/.local/lib/python3.5/site-
packages/sqlalchemy/sql/elements.py", line 323, in
_execute_on_connection
return connection._execute_clauseelement(self, multiparams, params)
File "/home/leonardo/.local/lib/python3.5/site-
packages/sqlalchemy/engine/base.py", line 1010, in
_execute_clauseelement
compiled_sql, distilled_params
File "/home/leonardo/.local/lib/python3.5/site-
packages/sqlalchemy/engine/base.py", line 1146, in _execute_context
context)
File "/home/leonardo/.local/lib/python3.5/site-
packages/sqlalchemy/engine/base.py", line 1341, in
_handle_dbapi_exception
exc_info
File "/home/leonardo/.local/lib/python3.5/site-
packages/sqlalchemy/util/compat.py", line 202, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "/home/leonardo/.local/lib/python3.5/site-
packages/sqlalchemy/util/compat.py", line 185, in reraise
raise value.with_traceback(tb)
File "/home/leonardo/.local/lib/python3.5/site-
packages/sqlalchemy/engine/base.py", line 1139, in _execute_context
context)
File "/home/leonardo/.local/lib/python3.5/site-
packages/sqlalchemy/engine/default.py", line 450, in do_execute
cursor.execute(statement, parameters)
sqlalchemy.exc.IntegrityError: (psycopg2.IntegrityError) null value in
column "id" violates not-null constraint
DETAIL: Failing row contains (null, 19, Male
, Basilicata , 2017-10-25
09:40:42.880058).
[SQL: 'INSERT INTO "user" (age, gender, region, date_created) VALUES
(%(age)s, %(gender)s, %(region)s, CURRENT_TIMESTAMP) RETURNING
"user".id'] [parameters: {'region': 'Basilicata', 'gender': 'Male',
'age': 19}]

Related

SQLAlchemy DataError gives me 500 Internal Server Error

So I have built a Flask App which takes client feedback and sends it to a PostgreSQL database. The app runs. But when submitting the data, it gives me this DataError:
Traceback (most recent call last):
File "C:\Users\kenny\programming\orizon-sarl\ORIZON-Feedback\venv\lib\site-packages\sqlalchemy\engine\base.py", line 1247, in _execute_context
self.dialect.do_execute(
File "C:\Users\kenny\programming\orizon-sarl\ORIZON-Feedback\venv\lib\site-packages\sqlalchemy\engine\default.py", line 590, in do_execute
cursor.execute(statement, parameters)
psycopg2.errors.InvalidTextRepresentation: invalid input syntax for type integer: "Bouche à oreille"
LINE 1: ...1'::timestamp, 'Jonathan Test', 'Léon Di Caprio', 'Bouche à ...
^
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\kenny\programming\orizon-sarl\ORIZON-Feedback\venv\lib\site-packages\flask\app.py", line 2077, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\kenny\programming\orizon-sarl\ORIZON-Feedback\venv\lib\site-packages\flask\app.py", line 1525, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\kenny\programming\orizon-sarl\ORIZON-Feedback\venv\lib\site-packages\flask\app.py", line 1523, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\kenny\programming\orizon-sarl\ORIZON-Feedback\venv\lib\site-packages\flask\app.py", line 1509, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
File "C:\Users\kenny\programming\orizon-sarl\ORIZON-Feedback\app.py", line 66, in submit
db.session.commit()
File "C:\Users\kenny\programming\orizon-sarl\ORIZON-Feedback\venv\lib\site-packages\sqlalchemy\orm\scoping.py", line 162, in do
return getattr(self.registry(), name)(*args, **kwargs)
File "C:\Users\kenny\programming\orizon-sarl\ORIZON-Feedback\venv\lib\site-packages\sqlalchemy\orm\session.py", line 1036, in commit
self.transaction.commit()
File "C:\Users\kenny\programming\orizon-sarl\ORIZON-Feedback\venv\lib\site-packages\sqlalchemy\orm\session.py", line 503, in commit
self._prepare_impl()
File "C:\Users\kenny\programming\orizon-sarl\ORIZON-Feedback\venv\lib\site-packages\sqlalchemy\orm\session.py", line 482, in _prepare_impl
self.session.flush()
File "C:\Users\kenny\programming\orizon-sarl\ORIZON-Feedback\venv\lib\site-packages\sqlalchemy\orm\session.py", line 2496, in flush
self._flush(objects)
File "C:\Users\kenny\programming\orizon-sarl\ORIZON-Feedback\venv\lib\site-packages\sqlalchemy\orm\session.py", line 2636, in _flush
with util.safe_reraise():
File "C:\Users\kenny\programming\orizon-sarl\ORIZON-Feedback\venv\lib\site-packages\sqlalchemy\util\langhelpers.py", line 68, in __exit__
compat.raise_(
File "C:\Users\kenny\programming\orizon-sarl\ORIZON-Feedback\venv\lib\site-packages\sqlalchemy\util\compat.py", line 178, in raise_
raise exception
File "C:\Users\kenny\programming\orizon-sarl\ORIZON-Feedback\venv\lib\site-packages\sqlalchemy\orm\session.py", line 2597, in _flush
flush_context.execute()
File "C:\Users\kenny\programming\orizon-sarl\ORIZON-Feedback\venv\lib\site-packages\sqlalchemy\orm\unitofwork.py", line 422, in execute
rec.execute(self)
File "C:\Users\kenny\programming\orizon-sarl\ORIZON-Feedback\venv\lib\site-packages\sqlalchemy\orm\unitofwork.py", line 586, in execute
persistence.save_obj(
File "C:\Users\kenny\programming\orizon-sarl\ORIZON-Feedback\venv\lib\site-packages\sqlalchemy\orm\persistence.py", line 239, in save_obj
_emit_insert_statements(
File "C:\Users\kenny\programming\orizon-sarl\ORIZON-Feedback\venv\lib\site-packages\sqlalchemy\orm\persistence.py", line 1135, in _emit_insert_statements
result = cached_connections[connection].execute(
File "C:\Users\kenny\programming\orizon-sarl\ORIZON-Feedback\venv\lib\site-packages\sqlalchemy\engine\base.py", line 984, in execute
return meth(self, multiparams, params)
File "C:\Users\kenny\programming\orizon-sarl\ORIZON-Feedback\venv\lib\site-packages\sqlalchemy\sql\elements.py", line 293, in _execute_on_connection
return connection._execute_clauseelement(self, multiparams, params)
File "C:\Users\kenny\programming\orizon-sarl\ORIZON-Feedback\venv\lib\site-packages\sqlalchemy\engine\base.py", line 1097, in _execute_clauseelement
ret = self._execute_context(
File "C:\Users\kenny\programming\orizon-sarl\ORIZON-Feedback\venv\lib\site-packages\sqlalchemy\engine\base.py", line 1287, in _execute_context
self._handle_dbapi_exception(
File "C:\Users\kenny\programming\orizon-sarl\ORIZON-Feedback\venv\lib\site-packages\sqlalchemy\engine\base.py", line 1481, in _handle_dbapi_exception
util.raise_(
File "C:\Users\kenny\programming\orizon-sarl\ORIZON-Feedback\venv\lib\site-packages\sqlalchemy\util\compat.py", line 178, in raise_
raise exception
File "C:\Users\kenny\programming\orizon-sarl\ORIZON-Feedback\venv\lib\site-packages\sqlalchemy\engine\base.py", line 1247, in _execute_context
self.dialect.do_execute(
File "C:\Users\kenny\programming\orizon-sarl\ORIZON-Feedback\venv\lib\site-packages\sqlalchemy\engine\default.py", line 590, in do_execute
cursor.execute(statement, parameters)
sqlalchemy.exc.DataError: (psycopg2.errors.InvalidTextRepresentation) invalid input syntax for type integer: "Bouche à oreille"
LINE 1: ...1'::timestamp, 'Jonathan Test', 'Léon Di Caprio', 'Bouche à ...
^
[SQL: INSERT INTO feedback (created_at, customer, advisor, rating, plaisir, choisi, "trouvés", comments) VALUES (%(created_at)s, %(customer)s, %(advisor)s, %(rating)s, %(plaisir)s, %(choisi)s, %(trouvés)s, %(comments)s) RETURNING feedback.id]
[parameters: {'created_at': datetime.datetime(2022, 5, 3, 10, 55, 51), 'customer': 'Jonathan Test', 'advisor': 'Léon Di Caprio', 'rating': 'Bouche à oreille', 'plaisir': '6', 'choisi': 'Prix', 'trouvés': 'Service', 'comments': 'test'}]
(Background on this error at: http://sqlalche.me/e/9h9h)
127.0.0.1 - - [03/May/2022 10:56:23] "POST /submit HTTP/1.1" 500 -
And this is the database model:
d = datetime.datetime.now()
class Feedback(db.Model):
__tablename__ = 'feedback'
id = db.Column(db.Integer, primary_key=True)
created_at = db.Column(db.DateTime(timezone=True), default=(d - datetime.timedelta(microseconds=d.microsecond)))
customer = db.Column(db.String(200), unique=True)
advisor = db.Column(db.String(200), index=True)
rating = db.Column(db.Integer)
plaisir = db.Column(db.String(200))
choisi = db.Column(db.String(200))
trouvés = db.Column(db.String(200))
comments = db.Column(db.Text(500))
def __init__(self, customer, advisor, plaisir, choisi, trouvés, rating, comments):
self.customer = customer
self.advisor = advisor
self.rating = rating
self.plaisir = plaisir
self.choisi = choisi
self.trouvés = trouvés
self.comments = comments
And here you have the routes:
#app.route('/')
def index():
return render_template('index.html')
#app.route('/submit', methods=['POST'])
def submit():
if request.method == 'POST':
customer = request.form['customer']
advisor = request.form['advisor']
rating = request.form['rating']
plaisir = request.form['plaisir']
choisi = request.form['choisi']
trouvés = request.form['trouvés']
comments = request.form['comments']
print(customer, advisor, rating, plaisir, choisi, trouvés, comments)
if customer == '' or advisor == '':
return render_template('index.html', message='Veuillez saisir les champs obligatoires')
if db.session.query(Feedback).filter(Feedback.customer == customer).count() == 0:
data = Feedback(customer, advisor, rating, plaisir, choisi, trouvés, comments)
db.session.add(data)
db.session.commit()
return render_template('success.html')
return render_template('index.html', message='Vous avez déjà soumis un commentaire')
It seems that the order of how the input is set, is wrong. But I don't get why.
Your rating column is designed as integer value:
rating = db.Column(db.Integer)
but the actual value from form:
'rating': 'Bouche à oreille'
As you can see Bouche à oreille is not an integer value it is a string, not a number. I think you have some errors in your HTML form, and the submitted data is wrong.

list index out of range on sqlalchemy query

I'm getting an index error when running a query / piece of code that clearly doesn't have index errors.
#cognito_required
def put(self, stage: str):
data = Default.validator.parse_args()
table = user_client_association_table
db.session.query().\
filter(table.c.client_id == data["client_id"]).\
filter(table.c.user_id == data["user_id"]).\
update({"is_default": 1})
db.session.commit()
return {
"data": {
"code": 200,
"message": f"The default user_id has been updated for the client {data['client_id']}",
}
}, 200
This is the full traceback:
Traceback (most recent call last):
File "/home/ericson/Workspace/concil-v4-api/venv/lib/python3.8/site-packages/werkzeug/serving.py", line 323, in run_wsgi
execute(self.server.app)
File "/home/ericson/Workspace/concil-v4-api/venv/lib/python3.8/site-packages/werkzeug/serving.py", line 312, in execute
application_iter = app(environ, start_response)
File "/home/ericson/Workspace/concil-v4-api/venv/lib/python3.8/site-packages/flask/app.py", line 2464, in __call__
return self.wsgi_app(environ, start_response)
File "/home/ericson/Workspace/concil-v4-api/venv/lib/python3.8/site-packages/flask/app.py", line 2450, in wsgi_app
response = self.handle_exception(e)
File "/home/ericson/Workspace/concil-v4-api/venv/lib/python3.8/site-packages/flask_cors/extension.py", line 165, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "/home/ericson/Workspace/concil-v4-api/venv/lib/python3.8/site-packages/flask_restful/__init__.py", line 272, in error_router
return original_handler(e)
File "/home/ericson/Workspace/concil-v4-api/venv/lib/python3.8/site-packages/flask/app.py", line 1867, in handle_exception
reraise(exc_type, exc_value, tb)
File "/home/ericson/Workspace/concil-v4-api/venv/lib/python3.8/site-packages/flask/_compat.py", line 38, in reraise
raise value.with_traceback(tb)
File "/home/ericson/Workspace/concil-v4-api/venv/lib/python3.8/site-packages/flask/app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "/home/ericson/Workspace/concil-v4-api/venv/lib/python3.8/site-packages/flask/app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/ericson/Workspace/concil-v4-api/venv/lib/python3.8/site-packages/flask_cors/extension.py", line 165, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "/home/ericson/Workspace/concil-v4-api/venv/lib/python3.8/site-packages/flask_restful/__init__.py", line 272, in error_router
return original_handler(e)
File "/home/ericson/Workspace/concil-v4-api/venv/lib/python3.8/site-packages/flask/app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/ericson/Workspace/concil-v4-api/venv/lib/python3.8/site-packages/flask/_compat.py", line 38, in reraise
raise value.with_traceback(tb)
File "/home/ericson/Workspace/concil-v4-api/venv/lib/python3.8/site-packages/flask/app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "/home/ericson/Workspace/concil-v4-api/venv/lib/python3.8/site-packages/flask/app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/ericson/Workspace/concil-v4-api/venv/lib/python3.8/site-packages/flask_restful/__init__.py", line 468, in wrapper
resp = resource(*args, **kwargs)
File "/home/ericson/Workspace/concil-v4-api/venv/lib/python3.8/site-packages/flask/views.py", line 89, in view
return self.dispatch_request(*args, **kwargs)
File "/home/ericson/Workspace/concil-v4-api/venv/lib/python3.8/site-packages/flask_restful/__init__.py", line 583, in dispatch_request
resp = meth(*args, **kwargs)
File "/home/ericson/Workspace/concil-v4-api/src/services/cognito_auth.py", line 12, in check_params
return _validate_token(func, *args, **kwargs)
File "/home/ericson/Workspace/concil-v4-api/src/services/cognito_auth.py", line 31, in _validate_token
return func(*args, **kwargs)
File "/home/ericson/Workspace/concil-v4-api/src/resources/default/__init__.py", line 49, in put
db.session.query().\
File "/home/ericson/Workspace/concil-v4-api/venv/lib/python3.8/site-packages/sqlalchemy/orm/query.py", line 4033, in update
update_op = persistence.BulkUpdate.factory(
File "/home/ericson/Workspace/concil-v4-api/venv/lib/python3.8/site-packages/sqlalchemy/orm/persistence.py", line 1818, in factory
return BulkUD._factory(
File "/home/ericson/Workspace/concil-v4-api/venv/lib/python3.8/site-packages/sqlalchemy/orm/persistence.py", line 1691, in _factory
return klass(*arg)
File "/home/ericson/Workspace/concil-v4-api/venv/lib/python3.8/site-packages/sqlalchemy/orm/persistence.py", line 1812, in __init__
super(BulkUpdate, self).__init__(query)
File "/home/ericson/Workspace/concil-v4-api/venv/lib/python3.8/site-packages/sqlalchemy/orm/persistence.py", line 1651, in __init__
self.mapper = self.query._bind_mapper()
File "/home/ericson/Workspace/concil-v4-api/venv/lib/python3.8/site-packages/sqlalchemy/orm/query.py", line 425, in _bind_mapper
ezero = self._entity_zero()
File "/home/ericson/Workspace/concil-v4-api/venv/lib/python3.8/site-packages/sqlalchemy/orm/query.py", line 412, in _entity_zero
else self._query_entity_zero().entity_zero
File "/home/ericson/Workspace/concil-v4-api/venv/lib/python3.8/site-packages/sqlalchemy/orm/query.py", line 398, in _query_entity_zero
return self._entities[0]
IndexError: list index out of range
Any ideas of what could cause this?
The problem was that I was working with a table object. user_client_association_table is a Table object, and I needed to specify a model to be updated. So I defined a model just for this:
from db import db
import model
class UserClient(db.Model):
__bind_key__ = "DEV_BASIS"
__tablename__ = "tb_user_client"
__table_args__ = {'extend_existing': True}
ctid = db.Column(db.Integer, primary_key=True)
And then I imported it:
from model import UserClient
...
#cognito_required
def put(self, stage: str):
data = Default.validator.parse_args()
table = user_client_association_table
db.session.query(UserClient).\
filter(table.c.client_id == data["client_id"]).\
filter(table.c.user_id == data["user_id"]).\
update({"is_default": 0})
db.session.commit()
return {
"data": {
"code": 200,
"message": f"The default user_id has been updated for the client {data['client_id']}",
}
}, 200
Really obscure problem here, thanks #snakecharmerb

Flask SQLAlchemy Won't Create Record (DateTimeField)

I'm putting together a CRM, and I'm having a little trouble. Everything has been working just fine, until I tried passing a time with a DateTimeField with SQLAlchemy. Prior to adding the time field, the record would create and add to the database without any issues. After, however, I the record simply wouldn't create. The page would reload, and I'd have no validation errors. I've been plugging away at it, and I've finally received an error. All of the data is passing through fine, except the time. The time is being fed through as <wtforms.fields.core.DateTimeField object at 0x10fe67210>. How do I get around this issue? I feel like the solution is simple, and I'm going to feel dumb for not seeing it sooner. Your help is greatly appreciated!
Here is the entire error log:
sqlalchemy.exc.StatementError
sqlalchemy.exc.StatementError: (builtins.TypeError) SQLite DateTime type only accepts Python datetime and date objects as input.
[SQL: INSERT INTO tours (customer_fname, customer_lname, customer_phone, partner_fname, partner_lname, partner_phone, region, tour_date, tour_time, gift, timestamp, user_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)]
[parameters: [{'user_id': 1, 'partner_phone': 'jkljlkj', 'tour_time': <wtforms.fields.core.DateTimeField object at 0x10fe67210>, 'partner_lname': 'jkljlk', 'region ... (34 characters truncated) ... ': 'kljklj', 'gift': 'Cruise', 'customer_lname': 'lkjlk', 'customer_phone': 'jkljl', 'partner_fname': 'jlkjlk', 'timestamp': None, 'tour_date': None}]]
Traceback (most recent call last)
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1173, in _execute_context
context = constructor(dialect, self, conn, *args)
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/sqlalchemy/engine/default.py", line 808, in _init_compiled
param.append(processors[key](compiled_params[key]))
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/sqlalchemy/dialects/sqlite/base.py", line 759, in process
"SQLite DateTime type only accepts Python "
The above exception was the direct cause of the following exception:
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/flask/app.py", line 2463, in __call__
return self.wsgi_app(environ, start_response)
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/flask/app.py", line 2449, in wsgi_app
response = self.handle_exception(e)
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/flask/app.py", line 1866, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/flask/app.py", line 2446, in wsgi_app
response = self.full_dispatch_request()
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/flask/app.py", line 1951, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/flask/app.py", line 1820, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/flask/app.py", line 1949, in full_dispatch_request
rv = self.dispatch_request()
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/flask/app.py", line 1935, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/flask_login/utils.py", line 272, in decorated_view
return func(*args, **kwargs)
File "/Users/brandoncarr/Desktop/BlueSkyAdmin/app/routes.py", line 68, in new_tour
db.session.commit()
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/sqlalchemy/orm/scoping.py", line 162, in do
return getattr(self.registry(), name)(*args, **kwargs)
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 1036, in commit
self.transaction.commit()
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 503, in commit
self._prepare_impl()
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 482, in _prepare_impl
self.session.flush()
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 2479, in flush
self._flush(objects)
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 2617, in _flush
transaction.rollback(_capture_exception=True)
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/sqlalchemy/util/langhelpers.py", line 68, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 153, in reraise
raise value
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 2577, in _flush
flush_context.execute()
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/sqlalchemy/orm/unitofwork.py", line 422, in execute
rec.execute(self)
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/sqlalchemy/orm/unitofwork.py", line 589, in execute
uow,
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/sqlalchemy/orm/persistence.py", line 245, in save_obj
insert,
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/sqlalchemy/orm/persistence.py", line 1137, in _emit_insert_statements
statement, params
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 982, in execute
return meth(self, multiparams, params)
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/sqlalchemy/sql/elements.py", line 293, in _execute_on_connection
return connection._execute_clauseelement(self, multiparams, params)
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1101, in _execute_clauseelement
distilled_params,
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1176, in _execute_context
e, util.text_type(statement), parameters, None, None
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/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/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/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/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 152, in reraise
raise value.with_traceback(tb)
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1173, in _execute_context
context = constructor(dialect, self, conn, *args)
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/sqlalchemy/engine/default.py", line 808, in _init_compiled
param.append(processors[key](compiled_params[key]))
File "/Users/brandoncarr/.local/share/virtualenvs/BlueSkyAdmin-PG_RBAa1/lib/python3.7/site-packages/sqlalchemy/dialects/sqlite/base.py", line 759, in process
"SQLite DateTime type only accepts Python "
My view:
#app.route('/tours/new', methods=['GET', 'POST'])
#login_required
def new_tour():
form = ToursForm()
if form.validate_on_submit():
tours = Tours(customer_fname=form.customer_fname.data, customer_lname=form.customer_lname.data,
partner_fname=form.partner_fname.data, partner_lname=form.partner_lname.data,
customer_phone=form.customer_phone.data, partner_phone=form.partner_phone.data,
region=form.region.data, gift=form.gift.data, tour_time=form.tour_time, author=current_user)
db.session.add(tours)
db.session.commit()
flash('Tour has been created', 'success')
return redirect(url_for('tours'))
return render_template('create-tour.html', title='Create A New Tour', form=form)
My model:
class Tours(db.Model):
id = db.Column(db.Integer, primary_key=True)
customer_fname = db.Column(db.String(50))
customer_lname = db.Column(db.String(50))
customer_phone = db.Column(db.String(140))
partner_fname = db.Column(db.String(100))
partner_lname = db.Column(db.String(100))
partner_phone = db.Column(db.String(100))
region = db.Column(db.String(140))
tour_date = db.Column(db.DateTime())
tour_time = db.Column(db.DateTime, default=datetime.utcnow)
gift = db.Column(db.String())
timestamp = db.Column(db.DateTime())
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
def __repr__(self):
return '<Tours {}>'.format(self.customer_fname, self.customer_lname, self.customer_phone,
self.partner_fname, self.partner_lname, self.partner_phone, self.region, self.tour_date,
self.tour_time, self.gift, self.timestamp)
My form:
class ToursForm(FlaskForm):
customer_fname = StringField('Customer First Name', validators=[DataRequired()])
customer_lname = StringField('Customer Last Name', validators=[DataRequired()])
customer_phone = StringField('Phone Number', validators=[DataRequired()])
partner_fname = StringField('Partner First Name', validators=[DataRequired()])
partner_lname = StringField('Partner Last Name', validators=[DataRequired()])
partner_phone = StringField('Phone Number', validators=[DataRequired()])
# tour_date = DateTimeField('Tour Date')
tour_time = DateTimeField('Tour Time', format='%I:%M %p')
gift = SelectField('Gift Selected', choices=[('Cruise', 'Cruise'), ('Hotel Suite', 'Hotel Suite'), ('Airfare', 'Airfare'), ('Safari', 'Safari')])
region = SelectField('Region', choices=[('Select Region', 'Select Region'), ('Oceanside', 'Oceanside'), ('Scottsdale', 'Scottsdale'), ('Granite Bay', 'Granite Bay'), ('Laguna Hills', 'Laguna Hills')])
submit = SubmitField('Add Tour')
As with all the other fields, access tour_time field's data. Now you are passing the field itself as value.
tour_time=form.tour_time
should be
tour_time=form.tour_time.data

Can't set uuid primary field in postgresql table flask-sqlalchemy

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)

sqlalchemy multiple flush before commit

I want insert records to 3 tables. The codes is below.
sessionDB.autocommit = False
# insert Address
addrRecord = Addres(street=street, city=city, zip_code=zip_code)
sessionDB.add(addrRecord)
sessionDB.flush()
# insert customer
cRecord = Customer(email=email, passwords=password, kind='individual', aID=addrRecord.aID)
sessionDB.add(cRecord)
sessionDB.flush()
# insert Hcustomer
homeCRecord = HomeCu(cID=cRecord.cID, fname=first_name, lname=last_name, age=age, marriage=marriage, remain=remain)
sessionDB.add(homeCRecord)
print (addrRecord.city, cRecord.kind, homeCRecord.lname)
sessionDB.commit()
The Addres's key is aID and Customer's key is cID. They are auto increment. In Customer, there is a foreign key constrained by Addres's aID. HomeCu's foreign key cID is constrained by Customer's cID.
So I want to insert Addres, Customer, HomeCu sequentially, during which I have to get the keys without doing commit. I used flush() to get the keys but failed at commit operation. Why during the commit operation, the value like email, password become None, but the print sentence is doing well? I have been struggling on this for hours. You help will be greatly appreciated. Thanks a lot!!!
Here is the error log:
Traceback (most recent call last):
File
"/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/engine/base.py",
line 1193, in _execute_context context)
File
"/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/engine/default.py",
line 509, in do_execute cursor.execute(statement, parameters)
File
"/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymysql/cursors.py",
line 170, in execute result = self._query(query)
File
"/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymysql/cursors.py",
line 328, in _query conn.query(q)
File
"/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymysql/connections.py",
line 516, in query self._affected_rows =
self._read_query_result(unbuffered=unbuffered)
File
"/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymysql/connections.py",
line 727, in _read_query_result result.read()
File
"/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymysql/connections.py",
line 1066, in read first_packet = self.connection._read_packet()
File
"/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymysql/connections.py",
line 683, in _read_packet packet.check_error()
File
"/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymysql/protocol.py",
line 220, in check_error err.raise_mysql_exception(self._data)
File
"/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymysql/err.py",
line 109, in raise_mysql_exception raise errorclass(errno, errval)
pymysql.err.IntegrityError: (1048, "Column 'email' cannot be null")
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Volumes/Code/Previous
Content/grad/2018_2019/db/final/app/modelController.py", line 164, in
registerIndividual('534 Tilling St', 'New York', '15233',
'123#qq.com', '123', 'Zimo', 'Tang', 23, 755.23, 0)
File
"/Volumes/Code/Previous
Content/grad/2018_2019/db/final/app/modelController.py", line 129, in
registerIndividual sessionDB.commit()
File
"/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/orm/session.py",
line 943, in commit self.transaction.commit()
File
"/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/orm/session.py",
line 467, in commit self._prepare_impl()
File
"/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/orm/session.py",
line 447, in _prepare_impl self.session.flush()
File
"/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/orm/session.py",
line 2254, in flush self._flush(objects)
File
"/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/orm/session.py",
line 2381, in _flush transaction.rollback(_capture_exception=True)
File
"/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/util/langhelpers.py",
line 66, in exit compat.reraise(exc_type, exc_value, exc_tb)
File
"/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/util/compat.py",
line 249, in reraise raise value
File
"/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/orm/session.py",
line 2345, in _flush flush_context.execute()
File
"/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/orm/unitofwork.py",
line 395, in execute rec.execute(self)
File
"/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/orm/unitofwork.py",
line 560, in execute uow
File
"/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/orm/persistence.py",
line 181, in save_obj mapper, table, insert)
File
"/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/orm/persistence.py",
line 830, in _emit_insert_statements execute(statement,
multiparams)
File
"/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/engine/base.py",
line 948, in execute return meth(self, multiparams, params)
File
"/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/sql/elements.py",
line 269, in _execute_on_connection return
connection._execute_clauseelement(self, multiparams, params)
File
"/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/engine/base.py",
line 1060, in _execute_clauseelement compiled_sql, distilled_params
File
"/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/engine/base.py",
line 1200, in _execute_context context)
File
"/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/engine/base.py",
line 1413, in _handle_dbapi_exception exc_info
File
"/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/util/compat.py",
line 265, in raise_from_cause reraise(type(exception), exception,
tb=exc_tb, cause=cause)
File
"/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/util/compat.py",
line 248, in reraise raise value.with_traceback(tb)
File
"/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/engine/base.py",
line 1193, in _execute_context context)
File
"/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/engine/default.py",
line 509, in do_execute cursor.execute(statement, parameters)
File
"/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymysql/cursors.py",
line 170, in execute result = self._query(query)
File
"/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymysql/cursors.py",
line 328, in _query conn.query(q)
File
"/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymysql/connections.py",
line 516, in query self._affected_rows =
self._read_query_result(unbuffered=unbuffered)
File
"/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymysql/connections.py",
line 727, in _read_query_result result.read()
File
"/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymysql/connections.py",
line 1066, in read first_packet = self.connection._read_packet()
File
"/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymysql/connections.py",
line 683, in _read_packet packet.check_error()
File
"/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymysql/protocol.py",
line 220, in check_error err.raise_mysql_exception(self._data)
File
"/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymysql/err.py",
line 109, in raise_mysql_exception raise errorclass(errno, errval)
sqlalchemy.exc.IntegrityError: (pymysql.err.IntegrityError) (1048,
"Column 'email' cannot be null") [SQL: 'INSERT INTO customer
(cID, email, passwords, kind, aID) VALUES (%(cID)s, %(email)s,
%(passwords)s, %(kind)s, %(aID)s)'] [parameters: {'cID': 31,
'email': None, 'passwords': None, 'kind': None, 'aID': None}]
(Background on this error at: http://sqlalche.me/e/gkpj)
Process finished with exit code 1
addrRecord = sessionDB.query(Addres).filter(Addres.street == street).filter(Addres.city == city) \
.filter(Addres.zip_code == zip_code).first()
cRecord = Customer(email=email, passwords=password, kind='individual', aID=addrRecord.aID)
homeCRecord = HomeCu(fname=first_name, lname=last_name, age=age, marriage=marriage, remain=remain)
homeCRecord.customer = cRecord
sessionDB.add(cRecord)
sessionDB.add(homeCRecord)
I didn' t use flush finally. I think using flush for multiple time will cause error. I add the relationship in instance. Hope this can help someone.

Categories