1

I have two models in which these model both return by toDict: as follows,

Tableone Model

class Base(db.Model):
    __abstract__  = True


class Tableone(Base):
    __tablename__ = 'tableone'

    zxc = db.Column(db.VARCHAR(20),  nullable=False)
    asd = db.Column(db.VARCHAR(20),  nullable=False)
    qwe = db.Column(db.Integer,  nullable=False)

    tabletwo = db.relationship("Tabletwo", primaryjoin="foreign(Tabletwo.asd) == Tableone.bnm", uselist=True)

    def __init__(zxc, asd, qwe):

        self.zxc     = zxc
        self.asd    = asd 
        self.qwe = qwe

    def toDict(self):
        return { c.key: getattr(self, c.key) for c in inspect(self).mapper.column_attrs }

Tabletwo Model

class Base(db.Model):
    __abstract__  = True


class Tabletwo(Base):
    __tablename__ = 'tabletwo'

    iop = db.Column(db.VARCHAR(20),  nullable=False)
    jkl = db.Column(db.VARCHAR(20),  nullable=False)
    bnm = db.Column(db.Integer,  nullable=False)

    def __init__(iop, jkl, bnm ):

        self.iop     = iop
        self.jkl    = jkl 
        self.bnm = bnm

    def toDict(self):
        return { c.key: getattr(self, c.key) for c in inspect(self).mapper.column_attrs }

Both model has toDict function. When I access it via controller:

    def index():
        tre = session.query(Tableone, Tabletwo).outerjoin(Tableone.tabletwo).all()
        treArr = []
        for tr in tre:
            treArr.append(tr.toDict())
        return jsonify(treArr)

it gives me error of .... is not JSON serializable

It works good if I only use it without join method like this:

  tre = session.query(Tableone).all()

Or if I mention the join one by one is also working :

  tre = session.query(Tableone.iop, Tableone.jkl, Tableone.bnm,  Tabletwo.zxc).outerjoin(Tableone.tabletwo).all()

Even though the result is only multidimensional array. [['iop1','jkl1', 'bnm1', 'zxc1'],[...],[...],[...],]

But I need to fetch all data in join table without have to mention it one by one what to select, and also need the key to access it in response.

Please help me how to do it correctly. I am very new in python. Many thanks in advance.


[UPDATE 1]

I am just aware if I access it like this:

    for tr in tre:
        treArr.append(tr.Tabletwo.toDict()) # here
    return jsonify(treArr)

It throws error saying AttributeError: 'NoneType' object has no attribute 'toDict'

But if I access it like this

    for tr in tre:
        treArr.append(tr.Tableone.toDict()) # here
    return jsonify(treArr)

It gives me the result of TableOne only


[UPDATE 2]

I was going to try it with merge function :

reference merge-two-dictionaries

for tr in tre:
    merge_two_dicts(sim.Simses.toDict(), sim.Carrier.toDict()) 
...

def merge_two_dicts(x, y):
    z = x.copy()   # start with x's keys and values
    z.update(y)    # modifies z with y's keys and values & returns None
    return z

But the error is mention in [UPDATE 1]

Al Kasih
  • 887
  • 5
  • 24
  • 1
    Please include the traceback. A snippet of the error message is less useful. In`tre = session.query(Tableone, Tabletwo)` I'd imagine that `treArr.append(tr.toDict())` should raise an attribute error, because `tr` is a namedtuple-like object holding 2 entities. – Ilja Everilä Dec 18 '18 at 06:16
  • @IljaEverilä, thank you again. I updated the question – Al Kasih Dec 18 '18 at 06:19
  • Well, I found the problem. It is because of outerjoin, should be changed to join. So simple. – Al Kasih Dec 18 '18 at 07:36
  • The problem is fixed already! :) – Al Kasih Dec 18 '18 at 07:42

0 Answers0