librelist archives

« back to archive

[flask] open relation

[flask] open relation

From:
Brice Leroy
Date:
2012-11-26 @ 17:12
Hello everybody,

It's about brainstorming on an elegant solution.

I have N classes:

class Comment(Models):
  author_id = Integer
  comment = String
  creation_date = Date

class Picture(Models):
  author_id = Integer
  image = File
  creation_date = Date
...

now let say, I have a "follow" feature, allowing a user X to get updates
when Y (the user followed by X) does something (creepy...).

So far I came up with something like that:

class FeedItem(Model)
  table = String
  key = Integer
  creation_date = Date

  def url(self):
    #get object by querying self.table with self.key
    object = self.get_an_object(table=self.table, key=self.key)
    return object.view_url

and then add this property to Comment and Picture classes:

  @property
  def view_url(self):
    return url_for('view_function_name', self.id)

What is your way of dealing with this kind of "open" relationship items?

Re: [flask] open relation

From:
Mark Grey
Date:
2012-11-26 @ 17:36
Hi Brice,

From what package/module/ORM does "Model" come?  It would help to know what
you're using.

On Mon, Nov 26, 2012 at 12:12 PM, Brice Leroy <bbrriiccee@gmail.com> wrote:

> Hello everybody,
>
> It's about brainstorming on an elegant solution.
>
> I have N classes:
>
> class Comment(Models):
>   author_id = Integer
>   comment = String
>   creation_date = Date
>
> class Picture(Models):
>   author_id = Integer
>   image = File
>   creation_date = Date
>  ...
>
> now let say, I have a "follow" feature, allowing a user X to get updates
> when Y (the user followed by X) does something (creepy...).
>
> So far I came up with something like that:
>
> class FeedItem(Model)
>   table = String
>   key = Integer
>   creation_date = Date
>
>   def url(self):
>     #get object by querying self.table with self.key
>     object = self.get_an_object(table=self.table, key=self.key)
>     return object.view_url
>
> and then add this property to Comment and Picture classes:
>
>   @property
>   def view_url(self):
>     return url_for('view_function_name', self.id)
>
> What is your way of dealing with this kind of "open" relationship items?
>
>

Re: [flask] open relation

From:
Yaniv Aknin
Date:
2012-11-26 @ 18:06
Assuming what I believe is the most common option (SQLAlchemy), I think I'd
recommend the terrific Joined Table

Inheritance<http://docs.sqlalchemy.org/en/rel_0_7/orm/extensions/declarative.html#joined-table-inheritance>feature
in this case.

The items which may be posted (Picture, Comment, etc) should all inherit a
Feedable table, which will have the fields common to all feedable items
(possibly just a pk) and a field which will be managed by SQLAlchemy and
will determine which 'subclass' this row represents.



 - Yaniv


On Mon, Nov 26, 2012 at 7:36 PM, Mark Grey <mark.asperia@gmail.com> wrote:

> Hi Brice,
>
> From what package/module/ORM does "Model" come?  It would help to know
> what you're using.
>
>
> On Mon, Nov 26, 2012 at 12:12 PM, Brice Leroy <bbrriiccee@gmail.com>wrote:
>
>> Hello everybody,
>>
>> It's about brainstorming on an elegant solution.
>>
>> I have N classes:
>>
>> class Comment(Models):
>>   author_id = Integer
>>   comment = String
>>   creation_date = Date
>>
>> class Picture(Models):
>>   author_id = Integer
>>   image = File
>>   creation_date = Date
>>  ...
>>
>> now let say, I have a "follow" feature, allowing a user X to get updates
>> when Y (the user followed by X) does something (creepy...).
>>
>> So far I came up with something like that:
>>
>> class FeedItem(Model)
>>   table = String
>>   key = Integer
>>   creation_date = Date
>>
>>   def url(self):
>>     #get object by querying self.table with self.key
>>     object = self.get_an_object(table=self.table, key=self.key)
>>     return object.view_url
>>
>> and then add this property to Comment and Picture classes:
>>
>>   @property
>>   def view_url(self):
>>     return url_for('view_function_name', self.id)
>>
>> What is your way of dealing with this kind of "open" relationship items?
>>
>>
>

Re: [flask] open relation

From:
Brice Leroy
Date:
2012-11-26 @ 18:40
Sorry for not specifying, I wasn't sure it would matter, but yes, I use
SQLAlchemy. I will have a look a your link. BTW, I like the cascade delete
on relationship provided by SQLAlchemy but I'm not sure it would get
triggered in this case (like deleting a Comment would delete the feed
related it). I guess I can always add a delete event on any object that may
have an FeedItem related to it.


On Mon, Nov 26, 2012 at 10:06 AM, Yaniv Aknin <yaniv@aknin.name> wrote:

> Assuming what I believe is the most common option (SQLAlchemy), I think
> I'd recommend the terrific Joined Table 
Inheritance<http://docs.sqlalchemy.org/en/rel_0_7/orm/extensions/declarative.html#joined-table-inheritance>feature
in this case.
>
> The items which may be posted (Picture, Comment, etc) should all inherit
> a Feedable table, which will have the fields common to all feedable items
> (possibly just a pk) and a field which will be managed by SQLAlchemy and
> will determine which 'subclass' this row represents.
>
>
>
>  - Yaniv
>
>
> On Mon, Nov 26, 2012 at 7:36 PM, Mark Grey <mark.asperia@gmail.com> wrote:
>
>> Hi Brice,
>>
>> From what package/module/ORM does "Model" come?  It would help to know
>> what you're using.
>>
>>
>> On Mon, Nov 26, 2012 at 12:12 PM, Brice Leroy <bbrriiccee@gmail.com>wrote:
>>
>>> Hello everybody,
>>>
>>> It's about brainstorming on an elegant solution.
>>>
>>> I have N classes:
>>>
>>> class Comment(Models):
>>>   author_id = Integer
>>>   comment = String
>>>   creation_date = Date
>>>
>>> class Picture(Models):
>>>   author_id = Integer
>>>   image = File
>>>   creation_date = Date
>>>  ...
>>>
>>> now let say, I have a "follow" feature, allowing a user X to get updates
>>> when Y (the user followed by X) does something (creepy...).
>>>
>>> So far I came up with something like that:
>>>
>>> class FeedItem(Model)
>>>   table = String
>>>   key = Integer
>>>   creation_date = Date
>>>
>>>   def url(self):
>>>     #get object by querying self.table with self.key
>>>     object = self.get_an_object(table=self.table, key=self.key)
>>>     return object.view_url
>>>
>>> and then add this property to Comment and Picture classes:
>>>
>>>   @property
>>>   def view_url(self):
>>>     return url_for('view_function_name', self.id)
>>>
>>> What is your way of dealing with this kind of "open" relationship items?
>>>
>>>
>>
>


-- 
--
Brice

Re: [flask] open relation

From:
Yaniv Aknin
Date:
2012-11-26 @ 18:58
As far as I understand relational databases, which isn't *that* much, I'm
afraid, deleting a Comment doesn't necessarily have to delete the Feedable it
depends on - but deleting a Feedable will for sure delete the inheriting
Comment (or else the constraint that the Comment's primary key refers to
the Feedable s primary key will be violated).

I *think* it only makes sense to assume that if you will delete a Comment,
SQLAlchemy will be smart enough to issue the delete against the underlying
Feedable  and will let the database do the cascade.

This is more an SQLAlchemy question than Flask; fortunately, it's also an
easy thing to check.

 - Yaniv


On Mon, Nov 26, 2012 at 8:40 PM, Brice Leroy <bbrriiccee@gmail.com> wrote:

> Sorry for not specifying, I wasn't sure it would matter, but yes, I use
> SQLAlchemy. I will have a look a your link. BTW, I like the cascade delete
> on relationship provided by SQLAlchemy but I'm not sure it would get
> triggered in this case (like deleting a Comment would delete the feed
> related it). I guess I can always add a delete event on any object that may
> have an FeedItem related to it.
>
>
> On Mon, Nov 26, 2012 at 10:06 AM, Yaniv Aknin <yaniv@aknin.name> wrote:
>
>> Assuming what I believe is the most common option (SQLAlchemy), I think
>> I'd recommend the terrific Joined Table 
Inheritance<http://docs.sqlalchemy.org/en/rel_0_7/orm/extensions/declarative.html#joined-table-inheritance>feature
in this case.
>>
>> The items which may be posted (Picture, Comment, etc) should all inherit
>> a Feedable table, which will have the fields common to all feedable
>> items (possibly just a pk) and a field which will be managed by SQLAlchemy
>> and will determine which 'subclass' this row represents.
>>
>>
>>
>>  - Yaniv
>>
>>
>> On Mon, Nov 26, 2012 at 7:36 PM, Mark Grey <mark.asperia@gmail.com>wrote:
>>
>>> Hi Brice,
>>>
>>> From what package/module/ORM does "Model" come?  It would help to know
>>> what you're using.
>>>
>>>
>>> On Mon, Nov 26, 2012 at 12:12 PM, Brice Leroy <bbrriiccee@gmail.com>wrote:
>>>
>>>> Hello everybody,
>>>>
>>>> It's about brainstorming on an elegant solution.
>>>>
>>>> I have N classes:
>>>>
>>>> class Comment(Models):
>>>>   author_id = Integer
>>>>   comment = String
>>>>   creation_date = Date
>>>>
>>>> class Picture(Models):
>>>>   author_id = Integer
>>>>   image = File
>>>>   creation_date = Date
>>>>  ...
>>>>
>>>> now let say, I have a "follow" feature, allowing a user X to get
>>>> updates when Y (the user followed by X) does something (creepy...).
>>>>
>>>> So far I came up with something like that:
>>>>
>>>> class FeedItem(Model)
>>>>   table = String
>>>>   key = Integer
>>>>   creation_date = Date
>>>>
>>>>   def url(self):
>>>>     #get object by querying self.table with self.key
>>>>     object = self.get_an_object(table=self.table, key=self.key)
>>>>     return object.view_url
>>>>
>>>> and then add this property to Comment and Picture classes:
>>>>
>>>>   @property
>>>>   def view_url(self):
>>>>     return url_for('view_function_name', self.id)
>>>>
>>>> What is your way of dealing with this kind of "open" relationship items?
>>>>
>>>>
>>>
>>
>
>
> --
> --
> Brice
>
>

Re: [flask] open relation

From:
Brice Leroy
Date:
2012-11-28 @ 21:21
So here is the solution to my open relationship problem:

https://gist.github.com/4164619

I will also have to add an on_delete event to make sure the event got
deleted when the object it relates to get deleted.

Hope it helps others to get an insight on generic relation in SQLAlchemy +
Flask.



On Mon, Nov 26, 2012 at 10:58 AM, Yaniv Aknin <yaniv@aknin.name> wrote:

> As far as I understand relational databases, which isn't *that* much, I'm
> afraid, deleting a Comment doesn't necessarily have to delete the Feedable it
> depends on - but deleting a Feedable will for sure delete the inheriting
> Comment (or else the constraint that the Comment's primary key refers to
> the Feedable s primary key will be violated).
>
> I *think* it only makes sense to assume that if you will delete a Comment,
> SQLAlchemy will be smart enough to issue the delete against the underlying
> Feedable  and will let the database do the cascade.
>
> This is more an SQLAlchemy question than Flask; fortunately, it's also an
> easy thing to check.
>
>  - Yaniv
>
>
> On Mon, Nov 26, 2012 at 8:40 PM, Brice Leroy <bbrriiccee@gmail.com> wrote:
>
>> Sorry for not specifying, I wasn't sure it would matter, but yes, I use
>> SQLAlchemy. I will have a look a your link. BTW, I like the cascade delete
>> on relationship provided by SQLAlchemy but I'm not sure it would get
>> triggered in this case (like deleting a Comment would delete the feed
>> related it). I guess I can always add a delete event on any object that may
>> have an FeedItem related to it.
>>
>>
>> On Mon, Nov 26, 2012 at 10:06 AM, Yaniv Aknin <yaniv@aknin.name> wrote:
>>
>>> Assuming what I believe is the most common option (SQLAlchemy), I think
>>> I'd recommend the terrific Joined Table 
Inheritance<http://docs.sqlalchemy.org/en/rel_0_7/orm/extensions/declarative.html#joined-table-inheritance>feature
in this case.
>>>
>>> The items which may be posted (Picture, Comment, etc) should all
>>> inherit a Feedable table, which will have the fields common to all
>>> feedable items (possibly just a pk) and a field which will be managed by
>>> SQLAlchemy and will determine which 'subclass' this row represents.
>>>
>>>
>>>
>>>  - Yaniv
>>>
>>>
>>> On Mon, Nov 26, 2012 at 7:36 PM, Mark Grey <mark.asperia@gmail.com>wrote:
>>>
>>>> Hi Brice,
>>>>
>>>> From what package/module/ORM does "Model" come?  It would help to know
>>>> what you're using.
>>>>
>>>>
>>>> On Mon, Nov 26, 2012 at 12:12 PM, Brice Leroy <bbrriiccee@gmail.com>wrote:
>>>>
>>>>> Hello everybody,
>>>>>
>>>>> It's about brainstorming on an elegant solution.
>>>>>
>>>>> I have N classes:
>>>>>
>>>>> class Comment(Models):
>>>>>   author_id = Integer
>>>>>   comment = String
>>>>>   creation_date = Date
>>>>>
>>>>> class Picture(Models):
>>>>>   author_id = Integer
>>>>>   image = File
>>>>>   creation_date = Date
>>>>>  ...
>>>>>
>>>>> now let say, I have a "follow" feature, allowing a user X to get
>>>>> updates when Y (the user followed by X) does something (creepy...).
>>>>>
>>>>> So far I came up with something like that:
>>>>>
>>>>> class FeedItem(Model)
>>>>>   table = String
>>>>>   key = Integer
>>>>>   creation_date = Date
>>>>>
>>>>>   def url(self):
>>>>>     #get object by querying self.table with self.key
>>>>>     object = self.get_an_object(table=self.table, key=self.key)
>>>>>     return object.view_url
>>>>>
>>>>> and then add this property to Comment and Picture classes:
>>>>>
>>>>>   @property
>>>>>   def view_url(self):
>>>>>     return url_for('view_function_name', self.id)
>>>>>
>>>>> What is your way of dealing with this kind of "open" relationship
>>>>> items?
>>>>>
>>>>>
>>>>
>>>
>>
>>
>> --
>> --
>> Brice
>>
>>
>


-- 
Brice