我将SQLALCHEMY与ORM类一起使用如下:
注册类/表:
class Registration(db.Model):
""" Activation Registrations Model for storing user Registrations related details """
__tablename__ = "user_registrations"
RegistrationID = db.Column(db.Integer, nullable=False, unique=True, primary_key=True, autoincrement=True)
RegistrationType = db.Column(db.Enum('Local', 'Facebook', 'Google'), nullable=False, server_default=text('Local'))
UserID = db.Column(db.Integer, db.ForeignKey('users.UserID'), nullable=False)
Email = db.Column(db.String(255), nullable=False)
@orm.reconstructor
def init_on_load(self):
# private
self.__dao = DAO(db)
def __init__(self, registration_type, user_id, email):
# private
self.__dao = DAO(db)
# public
self.RegistrationType = registration_type
self.UserID = user_id
self.Email = email
def save(self):
stored_proc = 'pUserRegister'
parameter = [self.UserID, str(self.RegistrationType), str(self.Email)]
result = self.__dao.call_procedure(stored_proc, parameter)
return {'status': status, 'message': message, 'data': data}
用户类/表:
class User(db.Model):
""" User Model for storing user related details """
__tablename__ = "users"
UserID = db.Column(db.Integer, primary_key=True, autoincrement=True)
Email = db.Column(db.String(255), unique=True, nullable=False)
UserName = db.Column(db.String(255), unique=True, nullable=False)
# relationships
Registrations = db.relationship('Registration', backref='users', lazy='dynamic')
def __init__(self, email, username):
# private
self.__dao = DAO(db)
# public
self.Email = email
self.UserName = username
@orm.reconstructor
def init_on_load(self):
# private
self.__dao = DAO(db)
@staticmethod
def register(registration_type, user_id, email):
if user_id and user_id > 0:
# check if user exists
user = User.query.filter(User.UserID == user_id).first()
if not user:
return {'status': False, 'data': {'message': 'Invalid User'}}
# initiate Registration
reg = Registration(registration_type, user_id, email)
result = reg.save()
return result
DAO班级:
class DAO(object):
def __init__(self, db):
# Unpack Other Database Arguments Here
self.__db = db
engine = self.__db.engine
self.connection = engine.raw_connection()
def call_procedure(self, procedure, data=()):
try:
with self.connection.cursor(MySQLdb.cursors.DictCursor) as cursor:
cursor.callproc(procedure, data)
result = json.dumps(cursor.fetchall(), cls=DateTimeEncoder)
result = json.loads(result)
result = {"status": True, "data": result}
return result
except (MySQLdb.Error, MySQLdb.Warning) as e:
log.info('>>>>> Exception: %s <<<<<', e)
result = {'status': False, 'data': {'message': "ERROR: {}".format(e)}}
finally:
cursor.close()
self.connection.commit()
self.connection.close()
return result
所以我尝试执行我的代码如下:(仅用于测试获取用户ID 70)
user = User.query.filter(User.UserID == 70).first()
log.info('>>>>> debug1: %s <<<<<', user)
使用上面的代码,我可以预先获得用户对象 那我在下面做:
resp = User.register(registration_type='Facebook', user_id=None, email='asd@aaswrtr.com')
log.info('>>>>> debug2: %s <<<<<', resp['data']['UserID'])
上面的代码我可以看到存储过程正确执行,我可以从resp['data']['UserID']
获取新插入的ID,我可以看到MYSQL DB中记录的数据
然后我在下面执行以获取我刚刚使用SP插入的数据:
user = User.query.filter(User.UserID == resp['data']['UserID']).first()
log.info('>>>>> debug3: %s <<<<<', user)
问题:我在上一条语句中收到了 user = None 。 这里可能出了什么问题?