FranGoitia
FranGoitia

Reputation: 1993

SQL Alchemy. has many relation that returns a particular column

I have the following tables:

Campaigns
+----------------------------+-------------------------------------------------------------------+------+-----+-------------------+----------------+
| Field                      | Type                                                              | Null | Key | Default           | Extra          |
+----------------------------+-------------------------------------------------------------------+------+-----+-------------------+----------------+
| id                         | int(11) unsigned                                                  | NO   | PRI | NULL              | auto_increment |
| campaign_type_id           | int(11)                                                           
+----------------------------+-------------------------------------------------------------------+------+-----+-------------------+----------------+

CampaignsSiteList
+--------------+------------+------+-----+---------+-------+
| Field        | Type       | Null | Key | Default | Extra |
+--------------+------------+------+-----+---------+-------+
| campaign_id  | int(11)    | NO   | PRI | NULL    |       |
| site_list_id | int(11)    | NO   | PRI | NULL    |       |
+--------------+------------+------+-----+---------+-------+

I'm using SQL Alchemy and I want to create a relationship so that objects of class Campaign have an attribute that return the list of site_list_id associated with them. I don't want the relation to return the list of CampaignSiteList objects, but a list that contains the column site_list_id of CampaignsSiteList.

Upvotes: 0

Views: 64

Answers (1)

reptilicus
reptilicus

Reputation: 10397

You could just use a property on the class and pull them out yourself, something like:

class Campaigns():

   # column definitions here

   sites = relationship("CampaignSiteList", lazy="joined")

   @property
   def site_ids(self):
       return [d.id for d in self.sites]

Upvotes: 2

Related Questions