csa3d
And it wasn't done so nicely
Alexander
So what you want to get from Object.enum if that specific enum value was deprecated?
csa3d
I want Object entry to remain even if there is no longer that enum
csa3d
So that's telling me it is optional
csa3d
Even if 99% of the time it is there
Alexander
But what do you want to get from Object.enum for deprecated values? None? Previous Enum object?
csa3d
None is fine.. Really don't care
csa3d
It will be red flag regardless
Alexander
If None is fine, than it should be declared as Optional
Alexander
Even if 99% of the time it's required
csa3d
Perfect. Sorry if that was a confusing circle
Alexander
Or you can declare entities as class Enum(db.Entity): objects = Set("Object") deprecated = Required(bool, default=False) class Object(db.Entity): enum = Optional("Enum", cascade_delete=False) And work only with enums which retrieved as select(e for e in Enum if not e.deprecated) In that case instead of deleting you just set deprecated value to True
Jim
Hi. I'm thinking to store rgba colors. Is it efficient to create a composite primary key (or composite index ?) with the 4 values ?
Alexander
Hi. I'm thinking to store rgba colors. Is it efficient to create a composite primary key (or composite index ?) with the 4 values ?
I think you can, but probably it will be more convenient to encode it to a single 32-bit value, this way linked tables will have only one column in foreign key, and queries will look more concise. Maybe something like this: class Color(db.Entity): rgba = PrimaryKey(int, size=32) r = Required(int, size=8) g = Required(int, size=8) b = Required(int, size=8) a = Required(int, size=8)
Jim
thank you alexander
Alexander
Hey I have a question about delete hooks. Assume I have a many to many relationship between a and b and I want a cascading deletion between them in the sense that if after a deletion of a any b that that has an empty a set should also be deleted. I wondered how to do that and stumbled upon deletion hooks. but when I try to delete the "to be orphaned" bs in a's before delete hook I get the exception that a is marked for deletion. I worked around this by manually deleting b afterwards but i would prefer something automatic. What's the best way to solve this in pony
Hi Jan! It is too late to do this in before_delete hook, because at this moment the object is already disconnected from related objects. You need to do it before actual call to delete. This should work: from pony.orm import * db = Database('sqlite', ':memory:') class Foo(db.Entity): name = Required(str) bars = Set('Bar') def delete_with_bars(self): bars = list(self.bars) self.delete() for bar in bars: if not bar.foos: bar.delete() class Bar(db.Entity): name = Required(str) foos = Set('Foo') db.generate_mapping(create_tables=True) sql_debug(True) with db_session: f1 = Foo(name='f1') f2 = Foo(name='f2') f3 = Foo(name='f3') b1 = Bar(name='b1', foos=[f1, f2]) b2 = Bar(name='b2', foos=[f1]) b3 = Bar(name='b3', foos=[f1, f2, f3]) with db_session: f1 = Foo[1] f1.delete_with_bars() with db_session: Foo.select().show() Bar.select().show()
Alexander
id|name --+---- 2 |f2 3 |f3 id|name --+---- 1 |b1 3 |b3
Jim
class Color(db.Entity): rgba = PrimaryKey(int, size=32) def test_init(self, ddb): a = ddb.Color(rgba=4278222848)ValueError: Value 4278222848 of attr Color.rgba is greater than the maximum allowed value 2147483647it has to be set unsigned
Alexander
add unsigned=True
Jim
no probleme with sqlite ?
Alexander
I think no, because SQLite uses 64 bit signed values
Alexander
Or you can use hex string as a primary key. Not as compact, but easier to read
Jim
I think I'll keep your first idea. having a 32bit primarykey is very practical and I use QColor (of Qt lib) for all the conversions sor it's pretty easy. class Color(db.Entity): rgba = PrimaryKey(int, size=32, unsigned=True) r = Required(int, size=8, unsigned=True) g = Required(int, size=8, unsigned=True) b = Required(int, size=8, unsigned=True) a = Required(int, size=8, unsigned=True, default=255) def __init__(self, value): color = QColor.fromRgba(value) rgba = color.rgba() r, g, b, a = color.toTuple() super().__init__(rgba=rgba, r=r, g=g, b=b, a=a) def test_init(self, ddb): a = ddb.Color(4278222848) flush() assert a.r == 0 assert a.g == 128 assert a.b == 0 assert a.a == 255
Alexander
looks good
Jonah
Hi @metaprogrammer I've been making progress on the continuous migration platform. Can you guys offer me some insight into migrations - what it will look like within pony/editor etc. Thanks!
Alexey
hey @minskmaz where are you located?
Jonah
Hey Alexey I'm in East Bay - next to San Francisco California
Alexey
Oh, cool Let me pm you
Genesis Shards Community
hi, good night, why group by in ponyorm?
Genesis Shards Community
select count(*) from customer where country='PE'
Alexander
from pony.or, import select, count count(c for c in Customer if c.country=='PE') or select(c for c in Customer if c.country=='PE').count() or Customer.select(lambda c: c.contry=='PE').count()
Genesis Shards Community
adhering a group by?
Alexander
select( (x.a, x.b, count(x.c), sum(x.d)) for x in X if x.a > 10 and avg(x.e) > x.z ) => SELECT x.a, x.b, COUNT(x.c), SUM(x.d) FROM x WHERE x.a > 10 GROUP BY x.a, x.b, x.z HAVING avg(x.e) > x.z Pony looks which expressions in SELECT were not aggregated, and add them to GROUP BY
Genesis Shards Community
select count(idcustomer) from customer where country='PE' group by idcompany
Alexander
select((customer.company, count(customer)) for customer in Customer if customer.country == 'PE')
Alexander
if you add something to GROUP BY, you most probably want to add it to SELECT too
Genesis Shards Community
thannks
Alexander
Sure
Anonymous
I am exploring Pony ORM because I've just had it with maintaining so much raw SQL lol... is there a recommended way of handling (including creation) table partitions?
Anonymous
For example here is the definition of one of my tables from some raw SQL CREATE TABLE IF NOT EXISTS auction_data( realm_id int NOT NULL REFERENCES realms(id) ON DELETE CASCADE, item int NULL, variant_id text NULL, pet_species int NULL, bid bigint NULL, buyout bigint NULL, quantity int NOT NULL ) PARTITION BY LIST (realm_id);
Anonymous
From what I can tell it looks like I might have to maintain the RAW Sql but just for the table creation and only for tables with partitions
Alexander
Hi! I think you can use table options for this: https://docs.ponyorm.org/api_reference.html?highlight=_table_options_#_table_options_ class Foo(db.Entity): a = Required(int) b = Required(str) _table_options_ = { 'PARTITION': 'BY LIST(a)' } But Pony expects that each table has primary key, and your table doesn't
Anonymous
Ya thats easily fixable and something I saw in the Pony docs
Anonymous
also thats brilliant and easy - I googled for like an hour before coming here lol
Anonymous
the multi-column unique key in that table would be (realm_id, item, variant_id, pet_species)
Anonymous
Project is still very much in dev so I've been in the process of doing a pass on keys and indexes also as part of switching to Pony
Anonymous
thanks so much @metaprogrammer
Alexander
Sure
Jim
hi, select((p.activite, p) for p in cls if p.activite.matiere.id == matiere_id).order_by(desc(Page.modified)) I want group my result by activite. Error : NotImplementedError: Ordering by attributes is limited to queries which return simple list of objects. Try use other forms of ordering (by tuple element numbers or by full-blown lambda expr).
Alexander
Hi 1) if you want to group result by p.activite than you should not place plain p without aggregation into select list (expression part of generator) 2) For ordering you can use .order_by(lambda p: desc(p.modified)) But not all databases support ordering by expression which is not presented in select list
Jim
Thank you Alexander.
Young
how can i make a migrations to add a row that i've just added to the class and i want to recreate this field in the database?
Alexander
Show the definition of the attribute you added
Young
i added the "genres" attribute
Young
Young
want to do the migration to add this field to the db
Alexander
A moment
Alexander
alter table movie add column genres text[] not null
Young
so i need to write the sql sentences by myself? im coming from others orm like entity framework, there is no a command with i can do the migrations automatically?
Alexander
Pony don't have official migration support yet, we are working on it In the meantime you can try a tool written by @luckydonald https://github.com/luckydonald/pony_up
Anonymous
Hi, just a question about the online editor. According to the pricing information the basic plan includes unlimited private diagrams. I purchased it yesterday and I see now, that only 5 private diagrams are included. What kind of trap is this? 😕
Lucky
@metaprogrammer?
Anonymous
Hi, thanks for the quick action. Appreciate it.
Anonymous
Hi again, I noticed a strange behavior in a new model I created. I created a new class with lots of fields, parts of them required, parts of them optional. After creating the corresponding table with generate_mapping() I tried to fill it with data and got the response, that those optional fields cannot be empty. So I had to add the option nullable=True to every optional field. Anyone else recognized such a thing? Regards, Thomas
Alexander
Were those fields of str type by any chance?
Anonymous
date, str, bool. I first thought this might be since I do not import everything from Pony with from pony.orm import *, I rather use from pony import orm and then call orm.Optional. But I proofed that it does not make any difference.
Alexander
Could you please provide minimal example to reproduce? I'll take a look today.
Anonymous
class ReleaseDetails(db.Entity): """Model for release details""" id = orm.PrimaryKey(int, auto=True) product = orm.Required(str, 20) version = orm.Required(str, 10) releasetyp = orm.Required(str, 20) change_architecture= orm.Optional(bool, default=False) training= orm.Optional(bool, default=False) test_from = orm.Optional(date, nullable=True) test_to = orm.Optional(date, nullable=True) used_environ = orm.Optional(str, nullable=True)
Alexander
Ah, so you created table by yourself? Pony's Optional str is NOT NULL. You can read more about this here: https://docs.ponyorm.org/api_reference.html#optional-string-attributes
Anonymous
But isn't this illogical? I mean I understand when a column marked as Required will draw an error, but an Optional column? Strange anyway is that I deliver an empty string to PonyORM (i.e. {'test_from':''} and therefore I have a ConstraintError. The same for date fields.
Anatoliy
You try set datetime with empty string
Alexander
Optional column can contain a special type of value - an empty value. For strings a standard empty value is '' NULL is a second empty value It is dangerous to allow two different empty values for a single column because for many queries the result of SELECT can look counter-intuitive if a person who write a query does not understand all tricky peculiarities of Three-Value Logic used for NULL values https://en.wikipedia.org/wiki/Three-valued_logic Like, if you write a query with condition AND last_name <> 'Smith' it will not return rows where last_name is NULL, because NULL, as unfortunately decided SQL designers means not "the value is absent", but "the value is unknown, and so can actually be Smith as well" So in order to avoid counter-intuitive query results we are trying to avoid using NULL if possible by default. Other ORMs do this as well - you can see that Django, for example, have two different options for string attributes - blank=True and null=True, and the former does not assume the latter https://stackoverflow.com/questions/7565073/django-null-and-empty-string
Alexander
https://docs.djangoproject.com/en/dev/ref/models/fields/#null
Alexey
#docs