sql - how to define sqlalchemy orm models for difficult logic -


I'm using sqlalchemy for my project, I have difficult arguments to define the model.

I have four database tables

  item_type - item_type_id (tinyint) - item_type_name (varchar) - Item_types "post (1)", "Products (2)", "Gallery (3)" tag - Tag_ id (Int) - Tag_nam (varchar) item_tag - item_type_id (tinyint) - item_id (int) - the Item_id be my model  
 < code> class tag (base): __tablename__ = 'Post_aidi or Utpad_aidi or Galri_aid - Tag_aid (int) Post - Post_aid - Post_taitl - Post_ content   

tag "Tag_ id = column (integer, Prathamik_ki = true ) Tag_names = column (string (255)) square post (b) Express): __tablename__ = 'post' post_id = column (integer, Prathamik_ki = true) will post_title = column (string (255)) Post_title = column (text) # tag links here Item Mdgred (base): __tablename__ = 'item_tag' item_type_id = Column (integer, primary_key = true) item_id = column (integer, primary_key = true) tag_ id = column (integer, foreign key ("tag tag_ide"), primary_key = true)

Getting the post tag sql (post_id = 12)

  SELECT TAG. * Tag tag on item_tag IT inner tag IT.tag_id = TAG.tag_id where IT.item_type_id = 1 and IT item_id = 12   

I do not know in the post model for tags How to define relationships. What is the correct way to post tags?

You can use primaryjoin to specify complex inserted positions For the orm.relationship () argument:

For your specific case, you will define something like:

  sqlalchemy import orm post from the class (base): ... tag = orm.relationship ('tag', primary join = 'and_ (ItemTag.tag_id == Tag.tag_id, ItemTag Item_id == Post.post_id, ItemTag.item_type_id == 1) ')   

I have not tested this A. This may require some modifications but hope to track it is enough for you to get right.

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -