Lucky
Oh
Alexey
you check Message.feed_entry and Message.link.feed_entry relationships
Lucky
This is so short.
I guess I was thinking way to complex
Alexey
each instance of Message can have one of those two relationships, right?
Lucky
Yep
Alexey
so you need to select the messages where one of the related FeedEntry instances has the status equal to 'UNREAD'
Lucky
And it won't get None errors on m.feed_entry.*?
Alexey
Nope
Alexey
it will select only those messages, which have this relationship
Lucky
Or right it is translating it first. So not real python properties. Always forget that.
Lucky
Cool
Lucky
Many thanks!
Alexey
Roman
Alexey
AFAIK it could be any primary key
Correct, this was an example of extracting an instance of FeedEntry. You could retirew it in any other way, for example using FeedEntry.get() https://docs.ponyorm.com/api_reference.html#Entity.get
Serge
Guys it looks like questions like this could be collected on a special web page with the answers and examples
Serge
This would be very handy
Alexey
Roman
Hi is talking about community provided faq I guess
Serge
may be a wiki or something like that
Serge
or... this is great and hacky:) you may post questions on StackOverflow and answer them, even if they wasn't originally asked there
Serge
this will allow to google an answer and spread PonyORM on SO at the same time:)
Alexey
Roman
@lig11 You just want to create stackoverflow
Roman
oh, I miss “you may post questions on StackOverflow and answer them”
Lucky
Use the github wiki?
Mikki
Welcome!
Andrii
Hi guys!
Anonymous
To guys
Anonymous
Really happy to join you all
Alexander
Welcome
Anonymous
Thanks buddy
Lucky
Hey guys I have a problem with the display of the ORM online editor.
I found no good place to file an issue, so I went for the doc repo:
https://github.com/ponyorm/pony-doc/issues/1
Alexey
hey
right, we are going to fix that
and probably create a space for the Editor bugs
Lucky
Ive seen empty repos with a readme for that kind of usecase before.
Alexey
ok
Alexey
Thank you for reporing about the bug, Luckydonald!
Lucky
You're welcome :D
Lucky
Do you guys have a clue what
AttributeError: _session_cache_
could mean?
Lucky
This is my DBInitMessage class
https://github.com/luckydonald/pbft/blob/953d74f86258603be0a5b06824d92130af9849e8/code/db_proxy/database.py#L41
Lucky
And this is messages.InitMessage
https://github.com/luckydonald/pbft/blob/953d74f86258603be0a5b06824d92130af9849e8/code/node/messages.py#L57
Alexander
Wait a sec...
Alexander
I think the problem is in the fact that Entity.__init__ method was not called
I think you need to add
super(Message, self).__init__()
at the beginning of the Message.__init__ method
Alexander
Because the Message class is a mixin
Alexander
I think it is better to not define __init__ in your mixins at all, and don't have any additional attributes which are unknown to Pony. This is because an entity instance can be constructed in several completely different situations. One thing is when you create a new instance, but completely different story is when an instance is retrieved from the database. In that case instance initialization works in a different way, and by overriding __init__ you may broke internal logic
Serge
AFAK it is recomended way of naming in Python to use Mixin word in such a classes like class MessageMixin:
Lucky
So I made a mixin, I didn't know :D
I just have this InitMessage class, and want to dump it into a Database, but without modifying the original class.
I'll try the super()
Lucky
The DBInitMessage is subclassing messages.InitMessage and db.Entity, to have InitMessage instances which can be stored in a DB
Lucky
Updated images above with links to github
Lucky
I have the Message classes which already do stuff like building itself from json, and converting itself to json, for sending and receiving.
I now want a second program to additionally store them in a database.
To avoid to write all json-related stuff twice, it is just importing the Message classes from the first program.
So my idea was to make a subclass with the pony foo = Required stuff.
Any thoughts how I could realize that?
Lucky
Oh, now I think I understand.
class A(object):
def __init__(self):
super().__init__()
print("Inside class A init")
class B(object):
def __init__(self):
print("Inside class B init")
class C(A,B):
def __init__(self):
super().__init__()
print("Inside class C init")
So:
>>> C()
Inside class B init
Inside class A init
Inside class C init
Lucky
Alexander
I think it is more complex than that, if __init__ method expects some arguments
Lucky
Lucky
class A(object):
def __init__(self, foobar):
print("Inside class A init, foobar={}".format(foobar))
super().__init__()
print("After class A init, foobar={}".format(foobar))
class B(object):
def __init__(self):
print("Inside class B init")
super().__init__()
print("After class B init")
class C(A, B):
def __init__(self):
print("Inside class C init")
super().__init__("hey")
print("After class C init")
So:
In[33]: C()
Inside class C init
Inside class A init, foobar=hey
Inside class B init
After class B init
After class A init, foobar=hey
After class C init
Out[33]:
<__main__.C at 0x1095c1780>
Alexander
I prefer something like that:
class JsonMixin(object):
def to_json(self):
return {'type': self.type, 'value': self.value}
@classmethod
def from_json(cls, **kwargs):
return cls(**kwargs)
class Message(JsonMixin):
def __init__(self, type, value):
self.type = type
self.value = value
class DBMessage(JsonMixin, db.Entity):
type = Required(int)
value = Required(str)
This way we have two different classes Message and DBMessage. Message is for in-memory data, and DBMessage is for persistent data. The common Json functionality is placed inside separate mixin class which have no __init__ method
Lucky
I see
Lucky
Other question, how can I model this correctly:
Lucky
Lucky
https://editor.ponyorm.com/user/luckydonald/pbft
Lucky
I get errors like:
pony.orm.core.ERDiagramError: Attribute DBPrevoteMessage.value conflicts with attribute DBInitMessage.value because both entities inherit from DBMessage. To fix this, move attribute definition to base class
Lucky
But If i move value up to DBMessage that would mean DBProposeMessage has that too.
Alexey
You need to add the 'value' and 'node' attributes to the DBMessage entity and remove tnem from the descendand classes
Lucky
And set it to optional?
Alexey
yes
Alexey
or, create another entity in the middle of the hierarchy and inherit from it the entities that have this attribute
Lucky
but we have
node, leader, value (VoteMessage)
node, leader, (ProposeMessage)
node, value (InitMessage)
So inheration is not really possible, right?
Alexey
if you make them optional, it would work
Lucky
okey
Lucky
Lucky
🔨 1 new commits to pbft:master:
03dea8e: db proxy Database with mixins? by luckydonald
Lucky
Lucky
I just tried to remove the DBMessage top class, and added the stuff via mixins instead, I just learend about :D
Alexander
It seems that Entity.__init__ call was still missed
Lucky
But I'm not overriding that any more