I have two SQLAlchemy models, which one is referred by primary key.
This is my parent model:
try:
from sqlalchemy import Column, Integer, Enum, DateTime, String, ForeignKey
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import scoped_session, sessionmaker, relationship
from sqlalchemy.orm import validates, backref
import datetime
from lib.dbConnector import Session
from werkzeug.security import generate_password_hash
except ImportError as err:
raise err
Base = declarative_base()
class users_table(Base):
__tablename__ = 'users_table'
userid = Column(String(50), unique= True, primary_key= True, nullable= False)
username = Column(String(500), unique= True, nullable= False)
password = Column(String(500), nullable= False)
email = Column(String(500), nullable= False, unique= True)
role = Column(String(10), default= 'user', nullable= False)
created_on = Column(DateTime, nullable= False, default= datetime.datetime.utcnow)
updated_on = Column(DateTime, nullable= False, default= datetime.datetime.utcnow)
and this is my child table:
try:
from sqlalchemy import Column, Integer, Enum, DateTime, String, ForeignKey
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import scoped_session, sessionmaker, relationship
from sqlalchemy.orm import validates
import datetime
from lib.dbConnector import Session
except ImportError as err:
raise err
Base = declarative_base()
class user_status_table(Base):
__tablename__ = 'user_status_table'
id = Column(Integer, primary_key= True, nullable= False, unique= True, autoincrement=True)
userid = Column(String(50), ForeignKey('models.usersModel.users_table.userid'), nullable= False, unique= True)
status = Column(String(50), default= 'active', nullable= False)
updated_on = Column(DateTime, nullable= False, default= datetime.datetime.utcnow)
userid = relationship('models.usersModel.users_table.users_table', foreign_keys=userid)
While running my server I am getting the following error
"One or more mappers failed to initialize - can't proceed with initialization of other mappers. Triggering mapper: 'Mapper|user_status_table|user_status_table'. Original exception was: mapper"
Can anyone suggest what am I doing wrong here? And this is where I commit the transction
userdata = usersModel.users_table(userid, username, password, email, role)
try:
self.session.add(userdata)
self.session.flush()
userstatusdata = userstatusModel.user_status_table(userdata.userid, 'active')
self.session.add(userstatusdata)
self.session.commit()
except:
self.session.rollback()
raise
Before we go into answering your question, there are a few remarks:
I created two files with the code, cleaned up to remove things I was sure not to need. The usermodel.py contain the following:
The userstatus.py contains:
Some of the changes highlighted:
Base
variable, and imported the variable in userstatus.py, so both tables are in the same schema. I do not think you need to import usermodel, but I did not check__tablename__
attribute. This must not be prefixed with the classname.Let's test: