Christian
Nevermind, my bad! It should have been user = Required(lambda: User) - in my mind I wasn't working with Sets, as every LoanEntry can only have one user.
Ben
Hi! Is there any way to define the intermediary table name?
Alexander
class SomeEntity(db.Entity): foos = Set("AnotherEntity", table='table_name') class AnotherEntity(db.Entity): bars = Set("SomeEntity")
Alexander
Hi! Is there any way to define the intermediary table name?
Can you also say, if we have bad docs for this case?
Gilbert
👋
Gilbert
I’m having a hard time creating a relationship with the diagram tool
Gilbert
I don’t see how to link two tables together
Gilbert
unless is a paid feature/
Alexander
Hello, smth like this
Gilbert
what is a Set?
Gilbert
vs Required?
Volbil
what is a Set?
Set is one to many relationship
Volbil
For example single user can have multiple friends (set)
Gilbert
ok
Gilbert
thanks
Radim
which branch should I use for most up to date migratios, and are they working for simple cases, or is it broken? thx
Alexander
Hey. If you use postgresql you can use migrations_dev branch. For simple cases it tested with SQL generation for migrations. We used Pony to generate migrations SQL and Flyway to manage their order. I don't remember if migration management is tested or not.
Alexander
It also can work with MySQL and Oracle but has few obvious bugs in SQLite case.
Radim
luckily I am on postgres, thank you for response
Radim
is there some docs for branch migrations_dev as in orm-migrations? https://github.com/ponyorm/pony/tree/orm-migrations/pony/migrate
Radim
for migrations_dev it seems the api changed a bit
Radim
never mind , i figure it out of the code, for those interested ... from pony.orm.migrations.cli import migrate migrate(db, "make")
Alexander
Docs are not really published and to be honest ready yet
Radim
no problem - could you please show me a way how to display only SQL what will be changed?
Radim
or am I using it correctly? migrate(db, 'sql')
Radim
because i am getting following attr error
Radim
Traceback (most recent call last): File "/home/keskfrad/migr/./models.py", line 160, in <module> migrate(db, "sql") File "/home/keskfrad/pony/pony/orm/migrations/cli.py", line 62, in migrate apply(vdb, db, graph, args, sql_only=cmd == 'sql') File "/home/keskfrad/pony/pony/orm/migrations/cli.py", line 176, in apply apply_(db, migration) File "/home/keskfrad/pony/pony/orm/migrations/cli.py", line 131, in apply_ op.apply(vdb) File "/home/keskfrad/pony/pony/orm/migrations/operations.py", line 379, in apply self.apply_to_schema(vdb, entity, relations_attrs) File "/home/keskfrad/pony/pony/orm/migrations/operations.py", line 385, in apply_to_schema table = schema.create_entity_table(entity) File "/home/keskfrad/pony/pony/orm/migrations/dbschema.py", line 833, in create_entity_table attr = entity.get_attr(attrname) File "/home/keskfrad/pony/pony/orm/migrations/virtuals.py", line 161, in get_attr raise AttributeError(attr_name) AttributeError: switch
Radim
here is entity class Switch(db.Entity): orchestrator = Required(Orchestrator) name = Required(str, unique=True) net = Required(str) zones = Set(Zone) interfaces = Set("Interface")
Alexander
Can you show Interface entity?
Radim
sure
Radim
class Interface(db.Entity): pid = Required(str, 30) switch = Required(Switch) wwpns = Set(WWPN) composite_key(switch, pid)
Radim
py interpreter is 3.9
Alexander
Hm not sure. Need to think about it.
Radim
and migration just deletes one column out of Orchestrator
Alexander
Didn't work on migrations for quite long time
Alexander
It might depend on how you use it. Original flow is - make initial - apply - make changes - commit them - apply etc.
Radim
aha, I did just initial make, then did changes in models and did second make
Radim
so have migrations folder with 0000 and 0001
Alexander
Can you show both (maybe link with snippets)
Radim
and then I am trying to see sql changes
Radim
sure
Radim
0000_initial - https://pastebin.com/2Kwn86tG 0001 https://pastebin.com/80Xr0sdJ
Alexander
If you really didnt remove that attribute, that might be a bug. So what you can do here - is to rewrite that second migration and then call migrate.py apply --sql to see the SQL of it.
Alexander
About rewriting. We don't have documented API yet, but they look like this one. List of operations with arguments. All operations available at pony.orm.migrations.operations.
Alexander
Today we finished our Py 3.10 support development. So I think we can try to finish our migrations in a near future. But it's hard to tell cause it depends on free time of our main jobs. Which is unpredictable. I also be very thankful if you could open an issue about this case.
Radim
I have probably misused the tool since there is no docs, so I am not sure about filling an issue however I have achieved what I wanted thanks to your help! also py3.10 support, I really appreciate it, keep up the good work
Robert
Hey guys, For a Set from a To-Many relationship, what is the way to iterate over it? I found my_set.random(len(my_set)) is a list that I can iterate. Is this the way to do it?
Radim
you should be able to iterate just like python lists/sets
Robert
Thanks. I can do a for element in set as proposed, but I cannot do set[0]. That raises an error. Perhaps list(set)[0] works. I did not try that.
Radim
yes for indexing just convert to list, it works well.. list(my_set)[0]
Robert
Thanks
Robert
Another question: how do I store information in a join table? Of course I know how to do it in SQL, but in Pony, the join table is automatically and transparently created from a Many-to-Many relationship (Sets). But I need to store some information in that table (information that belongs to the relationship). How should that be done?
Alexander
In that case you need to define an intermediate entity: class Person(db.Entity): name = Required(str) hobbies = Set("PersonHobby") class Hobby(db.Entity): name = Required(str) participants = Set("PersonHobby") class PersonHobby(db.Entity): person = Required(Person) hobby = Required(Hobby) PrimaryKey(person, hobby) started_at = Required(datetime, default=datetime.utcnow)
Robert
Prefect. Thanks!
Ben
Hi! Is there a way to get the postgres type from an object attribute? For example I have an object Person that has a name and an age that I need to use in a raw SQL query, how can I know what postgres type pony is using to cast it properly in my SQL query? Thanks!
Alexander
You can use something like Person.name.converters[0].get_sql_type()
Ben
ohh nice thanks!
Ben
and can I get a list of the members of the Object that are actually attribute with an SQL type (and not some internal thing)?
Alexander
attrs = Person._attrs_with_columns_ for attr in attrs: converter = attr.converters[0] print(attr.name, attr.py_type, converter.get_sql_type())
Ben
niiice
Ben
thanks a lot! that's perfect
Ben
One question, unsure if expected behaviour or bug. I have two tables with a many to many relationship between them. Both table have the _table_ name set. When pony generates the intermediary table it uses the class name instead of the _table_ name in the relationship. In my specific case I have OldIssue(db.Entity): _table_ = 'Issue' and OldClient(db.Entity): _table_ = 'Client' and pony makes the intermediary table be OldClient_OldIssue instead of Issue_Client. Is that expected? If so, how can I manually set this intermediary table name?
Alexander
Currently Pony uses class names as a source when generating a table name, because table names specified manually for compatibility with a legacy schema can be weird, like class Person(db.Entity): _table_ = 'TBL_PERS_01' and combining these weird names to calculating a name of intermediary tables can lead to even more surprising and weird names. You can specify the name of intermediary table manually: class OldClient(db.Entity) _table_ = 'Client' issues = Set("OldIssue", table='intermediary_table_name')
Ben
Thanks that worked!
Radim
is it possible to add new wheel of latest pony to pypi?
Alexander
Alexander will do it soon
Radim
great!
Muhammed
Hi all, I want to keep multiple pony projects in the same sql database. Each project has tables with the same name (e.g. WebUser). Therefore, when naming the tables, I want to prefix each table name with the name of project it is in. At the same time I want to name the tables with pony orm's default naming, just have a prefix at the beginning of the table names. How can I do that?
Alexander
Hi! I think there is no easy way to do this
Alexander
Probably it is better to have a separate SQL database for each project
Volbil
This way you can use same instance of SQL db and name tables for different project differently (for example using prefix or etc)
Muhammed
Basically what I want to do is actually this. But I don't want to write it over and over for each table. So I want to do something like add this prefix to all table names once.
Volbil
Change prefix variable for each project
Volbil
This should do the trick
Muhammed
Maybe I didn't express myself correctly. If I do it this way, I still have to add _table_ to each table one by one. This is exactly what I don't want to do.