SqlAlchemy Relationship and Marshmallow - json

I am trying to return JSON or even a complete string of a returned one to many sqlalchemy query. I am using Marshmallow at this point to try do it but it keeps returning incomplete data
I have two models defined as :
class UserModel(db.Model):
__tablename__ = 'usermodel'
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True)
password = db.Column(db.String(120))
weekday = db.relationship('weekDay', cascade='all,delete-orphan', single_parent=True, backref=db.backref('usermodel', lazy='joined'))
class weekDay(db.Model):
__tablename__ = 'weekday'
id = db.Column(db.Integer, primary_key=True)
#Defining the Foreign Key on the Child Table
dayname = db.Column(db.String(15))
usermodel_id = db.Column(db.Integer, db.ForeignKey('usermodel.id'))
I have defined two schemas
class WeekdaySchema(Schema):
id = fields.Int(dump_only=True)
dayname = fields.Str()
class UserSchema(Schema):
id = fields.Int(dump_only=True)
username = fields.Str()
password = fields.Str()
weekday = fields.Nested(WeekdaySchema)
and finally I run the command (I pass the name in userName variable)
userlist = UserModel.query.filter_by(parentuser=userName).all()
full_schema = UserSchema(many=True)
result, errors = full_schema.dump(userlist)
print (result)
I print the result to see before I attempt to Jsonify it:
My weekday object is completely empty
'weekday': {}
Doesn anyone know how I can do this correctly

It's a one-to-many relationship and you must indicate it on UserSchema, like that
class UserSchema(Schema):
id = fields.Int(dump_only=True)
username = fields.Str()
password = fields.Str()
weekday = fields.Nested(WeekdaySchema, many=True)
read more on documentation

Related

SQLAlchemy relationship selection criteria

I have two model classes:
class Programs(db.Model):
__tablename__ = "programs"
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(100), nullable=False)
description = db.Column(db.String(100), nullable=False)
duration = db.Column(db.Integer, nullable=False)
date_created = db.Column(db.DATE, default=datetime.now())
created_by = db.Column(db.String(100))
program_sessions = db.relationship('Program_Session',backref='programs')
class Program_Session(db.Model):
__tablename__ = "program_session"
id = db.Column(db.Integer, primary_key=True)
session_title = db.Column(db.String(100), nullable=False)
session_description = db.Column(db.String(100))
session_year = db.Column(db.Integer)
program_id = db.Column(db.Integer, db.ForeignKey("programs.id"), nullable=False)
students = db.relationship('Student_Registration', backref='program_session')
date_created = db.Column(db.DATE, default=datetime.now())
created_by = db.Column(db.String(100))
I create an object of Programs with:
program = Programs.query.first()
Now I can access all the Program_Sessions from the selected Program:
print(pro.program_sessions)
Is it possible to subquery/query to retrieve only those Program_session in Program whose year is 2021?
Option-1: filter on 'python' (in memory)
Once you get all Program_Sessions (all_sessions = pro.program_sessions), you filter them by sessions_2021 = [item for item in all_sessions if item.session_year == 2021].
Needless to say, this is not efficient at all as lots of data will be loaded from the database to be immediately discarded.
Option2: use Dynamic Relationship Loaders
Define the relationship with lazy="dynamic", which will return a Query and hence you will be able to apply additional criteria to the query:
class Programs(db.Model):
# ...
program_sessions = db.relationship('Program_Session', backref='programs', lazy="dynamic")
program = Programs.query.first()
sessions_2021 = program.program_sessions.filter(Program_Session.year == 2021).all()
Option3: use orm.with_parent [BEST]
sessions_2021 = select(Program_Session).where(with_parent(program, Program_Session.programs)).where(Program_Session.year == 2021)
The answer is yes...
van's answer shows you options for playing with sqlalchemy's query mechanism. But what if you want to write this logic on the Programs class itself? That way anywhere you have a Programs object, you can access the filter.
You can do it in pretty plain python by altering the Programs class like so:
class Programs(db.Model):
__tablename__ = "programs"
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(100), nullable=False)
description = db.Column(db.String(100), nullable=False)
duration = db.Column(db.Integer, nullable=False)
date_created = db.Column(db.DATE, default=datetime.now())
created_by = db.Column(db.String(100))
program_sessions = db.relationship('Program_Session',backref='programs')
'''Here I add a filter that returns only the sessions for a particular year
'''
def program_sessions_by_year(self, year):
return filter(lambda ps: ps.session_year == year, self.program_sessions)
If you care about efficiency, you can get the database to do the filtering for you using a bit more sqlalchemy magic:
from sqlalchemy.orm import object_session
class Programs(db.Model):
__tablename__ = "programs"
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(100), nullable=False)
description = db.Column(db.String(100), nullable=False)
duration = db.Column(db.Integer, nullable=False)
date_created = db.Column(db.DATE, default=datetime.now())
created_by = db.Column(db.String(100))
program_sessions = db.relationship('Program_Session',backref='programs')
'''Improve efficiency by using DB's SQL engine to filter the object.
'''
def program_sessions_by_year(self, year):
return object_session(self)\
.query(Program_Session)\
.filter_by(session_year=year, program_id=self.id)\
.all()
Either way you can then write (where-ever you have a Program object):
# lets say you just want the first program
first_program = Programs.query.first()
# to get the program sessions by year 2021
first_program.program_sessions_by_year(2021)
There's probably a bunch of other ways you could do something like this. SqlAlchemy is a big library. For more background on my answer, have a look at the SQL expressions as Mapped Attributes docs.

SQLAlchemy lazy sa.select([])

I use column properties to refer to properties of other tables.
I'd like to be able to select arbitrary information from other models indepentend of the declaration order of the models.
Just like sa.orm.relationship declares its relation by strings.
The problem I face is, that property_of_a can not be initialized, because ModelB is not declared at that moment.
Here is a simplified (not working) example.
A working alternative for this example might use sa.ext.associationproxy
I do not think, that I can use association proxies because I'd like to be able to use CONCAT, GROUP_CONCAT and IF-THEN-ELSE Queries.
Is there a way to initialize a sa.select query lazy (for example by strings like sa.orm.relationship)
import sqlalchemy as sa
Base = sa.ext.declarative.declarative_base()
metadata = Base.metadata
class ModelA(Base):
__tablename__ = "model_a_table"
id = sa.Column(sa.Integer, primary_key=True)
name = sa.Column(sa.String(255), nullable=False, unique=True)
model_b_id = sa.Column(sa.ForeignKey('model_b_table.id', ondelete='SET NULL'), index=True)
model_b = sa.orm.relationship("ModelB")
property_of_a = sa.orm.column_property(sa.select([ModelB.name]).where(ModelB.id == model_b_id))
class ModelB(Base):
__tablename__ = "model_b_table"
id = sa.Column(sa.Integer, primary_key=True)
name = sa.Column(sa.String(255), nullable=False, unique=True)
model_a_id = sa.Column(sa.ForeignKey('model_a_table.id', ondelete='SET NULL'), index=True)
model_b = sa.orm.relationship("ModelA")
property_of_b = sa.orm.column_property(sa.select([ModelA.name]).where(ModelB.id == model_a_id))
sa.orm.configure_mappers()

Sqlalchemy - Insert into table if not exist in a column (Mysql)

I have a simple Table like this
class Employeecode(Base):
__tablename__ = 'employeecode'
id = Column(Integer, primary_key=True)
unique_code = Column(String(5), unique=True)
employe_name = Column(String(50))
designation = Column(String(50))
How i can create a function which will insert value into 'employeecode' table if 'unique_code' column doesn't have that value.
insert_unique(unique_code,employe_name,designation)
First you can store all previous unicode_value in a list and then you can check and run your query..
def function(**kwargs):
session = Session()
unique_list =[]
employeecodes = db.session.query(Employeecode).all()
for employee in employeecodes:
unique_list.append(employee.unique_code)
if kwargs['unique_code'] not in unique_list:
employee = Employeecode()
employee.unique_code = kwargs['unique_code']
employee.employe_name = kwargs['employe_name']
employee.designation = kwargs['designation']
session.commit()
retval = row2dict(employee)
session.close()
return retval
else:
pass

How to get current user in a models of Pylons?

I have model like this:
class CreatedMixin(DeclarativeBase):
__abstract__ = True
#declared_attr
def updated_by(cls):
return Column(Integer, ForeignKey('user.user_id',
onupdate="cascade", ondelete="restrict"),
onupdate=CURRENT_USER_ID)
updated_at = Column(DateTime, nullable=False, default=dt.now(),
onupdate=dt.now())
And auth with repoze.what-quickstart. How to get CURRENT_USER_ID?
I think you could do the following
identity = request.environ.get('repoze.who.identity')
and the gettin the user id
id = identity['user'].id

how to save data in a many to many relationship using turbogears and sqlalchemy

hi i have a many to many relationship between a user and a group.and i will like to add a user with many groups in my database.how do i do that if my database is as follows
user_group_table = Table('tg_user_group', metadata,
Column('user_id', Integer, ForeignKey('tg_user.user_id',
onupdate="CASCADE", ondelete="CASCADE")),
Column('group_id', Integer, ForeignKey('tg_group.group_id',
onupdate="CASCADE", ondelete="CASCADE"))
)
class Group(DeclarativeBase):
"""
Group definition for :mod:`repoze.what`.1
Only the ``group_name`` column is required by :mod:`repoze.what`.
"""
__tablename__ = 'tg_group'
#{ Columns
group_id = Column(Integer, autoincrement=True, primary_key=True)
group_name = Column(Unicode(16), unique=True, nullable=False)
display_name = Column(Unicode(255))
created = Column(DateTime, default=datetime.now)
#{ Relations
users = relation('User', secondary=user_group_table, backref='groups')
#{ Special methods
def __repr__(self):
return '<Group: name=%s>' % self.group_name
def __unicode__(self):
return self.group_name
#}
class User(DeclarativeBase):
"""
User definition.
This is the user definition used by :mod:`repoze.who`, which requires at
least the ``user_name`` column.
"""
__tablename__ = 'tg_user'
#{ Columns
user_id = Column(Integer, autoincrement=True, primary_key=True)
user_name = Column(Unicode(16), unique=True, nullable=False)
email_address = Column(Unicode(255), unique=True, nullable=False,
info={'rum': {'field':'Email'}})
display_name = Column(Unicode(255))
_password = Column('password', Unicode(80),
info={'rum': {'field':'Password'}})
created = Column(DateTime, default=datetime.now)
doing it this way however gives me an error
#expose()
def user_save(self, **kw):
user = User()
user.user_name = kw['user_name']
user.display_name = kw['display_name']
user.email_address = kw['Email']
user._password = kw['password']
user.groups.extend(kw['groups'])
DBSession.add(user)
DBSession.flush()
flash("successfully saved...")
flash(user)
redirect("/user_new")
pls help me solve this.thanks in advance
I believe the answer is in the error message that you havn't posted in the question. user.groups is a list of Group objects, while you assign a list of strings(?) got from form to it. Also I see no explicit DBSession.commit() call. Are you sure TurboGears will do it for you?