Matthew
I agree, I deleted it. Legacy systems always have surprises :)
Anonymous
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
You need to grant correct permissions to database user for connecting to the database and performing select
Anonymous
Thank you, I had only granted connect, doh!
Anonymous
How can I go about deleting the entries in a cross reference table? (Since it doesn't have an associated entity)
Anonymous
Should I just create the corresponding entity?
Anonymous
Seems they're deleted when you delete the entities they reference.
Alexander
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)
Anonymous
Nice, thanks =)
ichux
How can Admin join a group? You're already an _admin_!
Lucky
Lol.
kwerty
Hi guys. I apologize for not being on the topic. Can I filter by @property in the sqlalchemy model?
Alexander
Hi Mikhail! What do you mean?
kwerty
class User(db.model): name = db.Column(db.String) @property def changed_name(): return name + '111'
kwerty
for example I have a model User
Alexander
You mean, filter in SQLAlchemy or in PonyORM?
kwerty
in SQLAlchemy
kwerty
Sorry for off topic
Alexander
I think you need to use @hybrid_property: http://docs.sqlalchemy.org/en/latest/orm/extensions/hybrid.html
kwerty
Thank you very much
Anonymous
Is there a way to use entity inheritance without creating a table for the base class?
Alexander
At this moment, no
Anonymous
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)?
Anonymous
Like Student = gen_person_class() Student.courses = Optional(int)
Alexander
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)))
Anonymous
Wonderful I'm going to try that, thank you =)
Anonymous
It worked \o/
Alexander
There may be some problems with defining composite_key and composite_index this way, but it is doable
Anonymous
Ah I'll keep that in mind if I start using them
Matthew
Do I need to do anything special if I want an init method on an entity?
Matthew
I just want to declare an internal instance variable, that isn't related to the database
Matthew
aka self._x = 1
Matthew
ah, init will already be used for Entity instance creation
Matthew
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
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
Thanks, yeah it seems easiest to avoid setting custom variables within the object.
Ehsan
Hi, is there a document for basic Pony application in Flask-Security?
Ehsan
Similiar to this: https://pythonhosted.org/Flask-Security/quickstart.html#id5
stsouko
See interface of SQLAlchemySessionUserDatastore
stsouko
And implement it for pony
Anonymous
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)
Anonymous
1) raises an assertion error in sqlbuilding line 83, while 2) works fine. Should I open an issue or is it expected?
Matthew
is elapsed a timedelta?
Anonymous
Yes
Matthew
I would say it's a bug but I'm not a pony dev :)
Anonymous
Alright I'll open an issue then
Alexander
It is a bug indeed (no support for timedelta literals), please open an issue
Anonymous
Will do right after lunch
Neal
Lunch? why not dinner
Alexander
Idk I just have breakfast
stsouko
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')
stsouko
like this: B(a=A[1]) b = B[1] b.a = A[2]
Matthew
Yes
Shikogo
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
.lower() works
Shikogo
there's also .casefold() as I am reading now, I might use that
Shikogo
thanks
Shikogo
wait, casefold() doesn't seem to work
Shikogo
appears to be a limitation of Pony
Shikogo
guess I'm using .lower().
Jim
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.
Jim
I translated the error message from french
Alexander
What database do you use?
Jim
postgre
Alexander
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.
Alexander
You can execute SQL manually using SQL console or pgAdmin tool or programmatically using db.execute(sql)
Jim
ok thank you. is the branch orm-migration the next release ? is it usable now ?
Alexander
It should work for simple mugrations in PostgreSQL. The orm-migration branch is based on current release, currently I'm in process of merging it with the code of upcoming release. Current migration code should handle simple migrations like adding a new attribute, but not complex migrations like changing inheritance
Jim
ok thank you
Alexander
You are welcome
Jim
Hello, is there a common usage on naming module with entity definition like django models ?
Alexander
Yes, it is common with Pony to make a module models.py which looks something like that: from pony import orm db = orm.Database() class Foo(db.Entity): ... class Bar(db.Entity): ... And then import it from main.py: from models import db import settings db.bind(*settings.db_params) db.generate_mapping(create_tables=True)
Jim
OK good thank you
Matthew
Is there a suggested pattern for splitting models in a project into sub modules?
Matthew
It isn't obvious me how to have references to other modules when doing that
Alexander
Something like this: # base_models.py from pony import orm db = orm.Database() class Foo(db.Entity): bars = Set('Bar') # or Set(lambda: db.Bar) # another_models.py from pony import orm from base_models import db class Bar(db.Entity): foos = Set('Foo') # or Set(lambda: db.Foo) # all_models.py from base_models import db, Foo from another_models import Bar class Baz(db.Entity): name = Required(str) # main.py from pony import orm from all_models import db, Foo, Bar, Baz import settings db.bind(**settings.db_params) db.generate_mapping(create_tables=True) select(bar for bar in db.Bar if ...) Note that you can access entities as attributes of db object, somethimes it makes things easier
Alexander
Also, it may be convenient to define entities inside a function: # models.py from pony import orm import models1, models2 db = Database() models1.define_entities(db) models2.define_entities(db) # models1.py def define_entities(db): class Foo(db.Entity): bars = Set('Bar') # or Set(lambda: db.Bar) # models2.py def define_entities(db): class Bar(db.Entity): foos = Set('Foo') # or Set(lambda: db.Foo) # main.py import settings from models import db db.bind(**settings.db_params) db.generate_mapping(create_tables = True) select(bar for bar in db.Bar if ...)
Matthew
Ah I didn't realise about entities being on the db object, cool :)