Re: [sqlalchemy] Writing ORM from a complex sql statement

2019-06-10 Thread Desmond Lim
= [result[0] for result in results] posts = PostsModel.find_by_id_list(id_list=post_id_list) I'm just wondering, if there is an easy way to get the post object from the query instead of doing the above. Desmond On Tue, 11 Jun 2019 at 08:30, Desmond Lim wrote: > Hi Simon, > > Sorry I think

Re: [sqlalchemy] Writing ORM from a complex sql statement

2019-06-10 Thread Desmond Lim
join("nodes") .filter(Node.id.in_(node_ids)) .all() ) for p in posts: print("Post %s: Node IDs %s" % (p.id, [n.id for n in p.nodes])) Simon On Mon, Jun 10, 2019 at 1:22 PM Desmond Lim wrote: Hi Simon, The tables and sample data are below. So first I search the nodes tabl

Re: [sqlalchemy] Writing ORM from a complex sql statement

2019-06-10 Thread Desmond Lim
FERENCES "public"."projects"("uuid"); ALTER TABLE "public"."pn" ADD FOREIGN KEY ("post_id") REFERENCES "public"."posts"("id"); ALTER TABLE "public"."pn" ADD FOREIGN KEY ("node_

[sqlalchemy] Writing ORM from a complex sql statement

2019-06-09 Thread Desmond Lim
Hi there, I really have no idea how to do this via sqlalchemy. I have 2 tables: class NodesModel(db.Model): __tablename__ = 'nodes' id = db.Column(db.BigInteger, primary_key=True) topic = db.Column(db.String(20), nullable=False) pn = relationship("PNModel", backref="nodes")

[sqlalchemy] Returning all columns except two

2019-06-03 Thread Desmond Lim
Hi All, I have a question which I think I know the answer to but I'd like to confirm. I have a table/model that when I return the objects or list of objects, I want all the columns (15 of them) to be returned except 2. Is there a way to do that? Currently, I'm doing this (as per documentation)

Re: [sqlalchemy] Child table with 2 relationships to parent

2019-05-29 Thread Desmond Lim
" to tell SQLAlchemy that when you change the property on > one class, the corresponding change must be made to the property on > the other class. > > https://docs.sqlalchemy.org/en/13/orm/backref.html > > Simon > > > On Wed, May 29, 2019 at 1:00 PM Desmond Lim

Re: [sqlalchemy] Child table with 2 relationships to parent

2019-05-29 Thread Desmond Lim
p]) > session.flush() > > print(node1.targets) > > > Given a node, you can access the relationships which use that node as > a source via the backref "node.targets", and the relationships that > use that node as a target via "node.sources"

Re: [sqlalchemy] Child table with 2 relationships to parent

2019-05-29 Thread Desmond Lim
the same thing? 2. Why do we not use the foreign_keys method to link all the tables instead of backref in the parent table? Thanks. Desmond On Wed, 29 May 2019 at 18:38, Desmond Lim wrote: > Hi Simon, > > I've read and I've tried a number of what is written but I still can't > solve

Re: [sqlalchemy] Child table with 2 relationships to parent

2019-05-29 Thread Desmond Lim
) relationships_t = relationship("RelationshipsModel", foreign_keys=["relationships.target_node_id"], backref="nodes") I've also tried using relationships = relationship("RelationshipsModel", foreign_keys="[Nod

[sqlalchemy] Child table with 2 relationships to parent

2019-05-29 Thread Desmond Lim
Hi there, I'm been puzzling over this and still can't find answer. I have 2 tables: Nodes: class NodesModel(db.Model): __tablename__ = 'nodes' id = db.Column(db.BigInteger, primary_key=True) project_uuid = db.Column(UUID(as_uuid=True), db.ForeignKey('projects.uuid')) name =

Re: [sqlalchemy] Re: Is it good practice to copy all sqlalchemy models (tables) for every API I create?

2019-05-16 Thread Desmond Lim
As told by Jonathan, I’m trying to create a separate package for the models. Can anyone point me in the right direction to create the models as a module. I can’t seem to think how to do it because of the different ways you can connect to the database and there is the unit test to do. I just

Re: [sqlalchemy] Re: Is it good practice to copy all sqlalchemy models (tables) for every API I create?

2019-05-13 Thread Desmond Lim
Thanks Jonathan. Exactly what I wanted to know. Desmond On Tue, 14 May 2019 at 02:22, Jonathan Vanasco wrote: > Are all of these APIs for the same company/organization (e.g. in-house > services), or are you developing something for different companies (you are > an agency with clients)? > >

[sqlalchemy] Is it good practice to copy all sqlalchemy models (tables) for every API I create?

2019-05-12 Thread Desmond Lim
Hi there, I'm just wondering what is the best way to do this. I'm not asking about micro-services, just a general API coding practice. Currently, I have a number of APIs (separate code base) and each has their own model (e.g. users) and the endpoints create don't need anymore tables (models)