
Alexander
05.02.2018
16:46:43
> so that would be a massive index for the composite key?
Yes, but it should make select queries much faster and straightforward, and the queries probably will not lock another indexes, so the performance should be much faster at the expence of some disk space
Initially I thought that you can add another unique column hash with md5 hash value of serialized value for combined list of columns used for search and comparison. Then the index should be relatively small in size. But it will work if the total list of columns used for search is fixed and will not be extended later, or you will need to re-calculate md5 hash on adding new attribute

Matthew
05.02.2018
16:47:24
I never query on most of these columns, apart from duplicate detection

Alexander
05.02.2018
16:48:36
But the question is, how ofthen you need to perform duplicate detection

Matthew
05.02.2018
16:49:41
probably about 10 - 15 times per day per parent object, of which i have 6 figures

Google

Matthew
05.02.2018
16:50:06
so a few million times per day
I calculated that 60% of the rows in my big table could be considered duplicate, so my incentive is to shrink that table for performance and resource usage

Alexander
05.02.2018
16:53:53
I think having a single index to find a duplicate object should be a great boost for performance
I think it is worth to try
At first you can include parent attribute and all other attributes used for duplicate search (I hope it is always the same list of attributes for each query), and as a second option add indexed hash column with md5 hash of another column and use it for duplicate search

Matthew
05.02.2018
16:54:51
Thank you, I will think about it.
I have it with existing_object.to_dict() cached in redis now, which seems to work, so I will monitor it

Ehsan
05.02.2018
18:19:24
Hi, what is the best approach for appending data into an Entity in a for loop? Is it like incrementing the id of an entity and then using Person[id].age=...?

Matthew
05.02.2018
18:40:26
Create a new entity, Person(age=1) etc
@akozlovsky I had an index on "date" when I also had an index on (product, date) which seemed to be confusing postgres. This seemed to be causing a lot of the issues. Maybe I don't need to worry about accessing previous object attributes now

Alexander
06.02.2018
08:37:27
I think the first index is unnecessary

Matthew
06.02.2018
08:39:28
I agree, I deleted it. Legacy systems always have surprises :)

Etienne
06.02.2018
11:49:46
Is there a way to connect to a database with read only rights?
I'm trying but I get an error when I call generate mappings (programming error, permission denied for relation ...)

Alexander
06.02.2018
11:51:21
You need to grant correct permissions to database user for connecting to the database and performing select

Etienne
06.02.2018
11:54:44
Thank you, I had only granted connect, doh!

Google

Etienne
12.02.2018
09:21:58
How can I go about deleting the entries in a cross reference table? (Since it doesn't have an associated entity)
Should I just create the corresponding entity?
Seems they're deleted when you delete the entities they reference.

Alexander
12.02.2018
11:27:55
class Article(db.Entity):
name = Required(str)
tags = Set("Tag")
class Tag(db.Entity):
name = Required(str, unique=True)
articles = Set("Article")
with db_session:
article = Article[123]
tag = Tag.get(name='foo')
article.tags.remove(tag)

Etienne
12.02.2018
13:27:42
Nice, thanks =)

Chukwudi
14.02.2018
10:07:08
How can Admin join a group? You're already an _admin_!

Luckydonald
16.02.2018
08:41:29
Lol.

Mikhail
21.02.2018
11:21:03
Hi guys. I apologize for not being on the topic. Can I filter by @property in the sqlalchemy model?

Alexander
21.02.2018
11:21:49
Hi Mikhail! What do you mean?

Mikhail
21.02.2018
11:22:07
class User(db.model):
name = db.Column(db.String)
@property
def changed_name():
return name + '111'
for example I have a model User

Alexander
21.02.2018
11:22:42
You mean, filter in SQLAlchemy or in PonyORM?

Mikhail
21.02.2018
11:22:52
in SQLAlchemy
Sorry for off topic

Alexander
21.02.2018
11:24:01
I think you need to use @hybrid_property:
http://docs.sqlalchemy.org/en/latest/orm/extensions/hybrid.html

Mikhail
21.02.2018
11:24:15
Thank you very much

Etienne
21.02.2018
13:29:05
Is there a way to use entity inheritance without creating a table for the base class?

Alexander
21.02.2018
13:29:36
At this moment, no

Etienne
21.02.2018
13:31:06
Ah bummer, is there a way to add attributes to an entity class after having defined its class then? (But before binding and generating the mapping)?

Google

Etienne
21.02.2018
13:34:10
Like
Student = gen_person_class()
Student.courses = Optional(int)

Alexander
21.02.2018
13:41:32
It should be possible to create entity class using the metaclass API:
from pony.orm import *
from pony.orm.core import EntityMeta
db = Database()
Person = EntityMeta('Person', (db.Entity,), dict(name=Required(str), age=Required(int)))
This way you can generate some attributes dynamically:
def make_common_attributes():
return dict(
created_at=Required(datetime, default=datetime.now),
updated_at=Optional(datetime))
Person = EntityMeta('Person', (db.Entity,), dict(make_common_attributes(), name=Required(str), age=Required(int)))

Etienne
21.02.2018
13:42:25
Wonderful I'm going to try that, thank you =)
It worked \o/

Alexander
21.02.2018
13:59:34
There may be some problems with defining composite_key and composite_index this way, but it is doable

Etienne
21.02.2018
14:03:59
Ah I'll keep that in mind if I start using them

Matthew
22.02.2018
08:50:20
Do I need to do anything special if I want an init method on an entity?
I just want to declare an internal instance variable, that isn't related to the database
aka self._x = 1
ah, init will already be used for Entity instance creation
I actually want the variable to be assigned whenever an instance is either created or loaded from the database, what is the appropriate way to do that?


Alexander
22.02.2018
09:12:44
Currently it is possible to override __init__ method, but it only works when a new instance of entity is created, not during the loading from the database.
When the object is loaded from the database, it may be in a partially loaded state, when only some attributes are loaded. Also, sometimes when an object is instantiated from the database, it is not known what is the type of that object yet. For example, we have Comment.author attribute of Person type, and there are three classes Person, Student and Teacher where Student and Teacher are inherited from Person, and we have an object with specific id which may be a Person or some of its subclass, but we don't know exactly yet what specific class it has. This information may be confirmed later, when the object is requested by application-level code.
So, I think it may be nontrivial to provide a hook which will be called when an object is loaded from the database. It may be brittle and may lead to performance degradation in some cases.
Maybe it is better to make a map {object_id: {additional_info}} which you can populate whenever you access the object with that id


Matthew
22.02.2018
09:24:48
Thanks, yeah it seems easiest to avoid setting custom variables within the object.

Ehsan
01.03.2018
05:32:51
Hi, is there a document for basic Pony application in Flask-Security?
Similiar to this: https://pythonhosted.org/Flask-Security/quickstart.html#id5

stsouko
01.03.2018
06:03:46
See interface of SQLAlchemySessionUserDatastore
And implement it for pony

Etienne
05.03.2018
10:17:24
1)
orm.select(r for r in rows if r.elapsed <= timedelta(minutes=15))
2)
delta = timedelta(minutes=15)
orm.select(r for r in rows if r.elapsed <= delta)
1) raises an assertion error in sqlbuilding line 83, while 2) works fine. Should I open an issue or is it expected?

Matthew
05.03.2018
10:18:58
is elapsed a timedelta?

Google

Etienne
05.03.2018
10:19:07
Yes

Matthew
05.03.2018
10:19:33
I would say it's a bug but I'm not a pony dev :)

Etienne
05.03.2018
10:20:10
Alright I'll open an issue then

Alexander
05.03.2018
10:40:38
It is a bug indeed (no support for timedelta literals), please open an issue

Etienne
05.03.2018
10:52:17
Will do right after lunch

Neal
05.03.2018
10:52:42
Lunch? why not dinner

Alexander
05.03.2018
10:57:54
Idk I just have breakfast

stsouko
06.03.2018
09:23:28
Hello!
Is it possible to move entities in relation many to one?
class A(db.Entity):
s = Set('B')
class B(db.Entity):
a = Required('A')
like this:
B(a=A[1])
b = B[1]
b.a = A[2]

Matthew
06.03.2018
09:25:06
Yes

Shikogo
08.03.2018
14:28:15
can I select case insensitively? something like
select(item for item in SomeEntity if item.name == "somename")
but case insensitive? or should I just use .lower() on both ends?

Matthew
08.03.2018
14:28:28
.lower() works

Shikogo
08.03.2018
14:29:23
there's also .casefold() as I am reading now, I might use that
thanks
wait, casefold() doesn't seem to work
appears to be a limitation of Pony
guess I'm using .lower().

Jim
15.03.2018
21:29:17
hello. I know a migration tool is coming but for now how do yout add new attribute to an existing ? I always get an error : pony.orm.dbapiprovider.ProgrammingError: ERROR : The column myentity.new_attribute does not exist.
I translated the error message from french

Alexander
15.03.2018
21:30:42
What database do you use?

Google

Jim
15.03.2018
21:30:52
postgre

Alexander
15.03.2018
21:36:22
To create new column manually, you need to execute SQL command:
alter table myentity add new_attribute <type>
where <type> is text for str attribute, int for int attribute, etc.
You can execute SQL manually using SQL console or pgAdmin tool or programmatically using db.execute(sql)