Lucky
I made a migration script.
Can be found at
https://github.com/luckydonald/pony_up
Lucky
Permanent link to the luckydonald/pony_up project you mentioned. (?)
Anonymous
wow
Lucky
Lucky
And the file structure is very basic but working.
Lucky
How can I use the same in-memory SQLite database in two different .bind(...) statements?
Anonymous
Not directly related to Pony ORM, but what's the best way to generate a new database and create a user for this database?
Alexey
Anonymous
mysql
Alexey
https://www.mysql.com/products/workbench/
or console
Anonymous
Since I mean using python to generate this user and database for user I would need to use console right?
Alexey
do you want to create it one time, or you want to automate this process? it is not clear from your question
Anonymous
I would need to call this code from my application. It's for an API endpoint. Each time a new user orders a server a new database access (user) and a database needs to be created for this user.
Anonymous
Api endpoint itself is done, now I just wonder how to create database and user for this database
Alexey
http://stackoverflow.com/questions/33768386/create-a-mysql-database-in-python
Alexey
http://stackoverflow.com/questions/8932261/create-mysqldb-database-using-python-script
Anonymous
big thanks
Lucky
Lucky
Ah, probably using
'file::memory:?cache=shared' as filename
http://stackoverflow.com/a/15720872/3423324
Lucky
Seems you can even give them a name:
file:filename?mode=memory&cache=shared
file:foobar?mode=memory&cache=shared
https://www.sqlite.org/inmemorydb.html
Alexey
Don't think so
Alexey
http://stackoverflow.com/questions/3315046/sharing-a-memory-database-between-different-threads-in-python-using-sqlite3-pa
Alexey
Wait, read the second answer
Lucky
But PonyORM doesn't handle the file: protocol, it seems.
I logged it at https://github.com/ponyorm/pony/issues/260
.
Alexey
In the same thread you can call generate_mapping() only once
Alexey
Lucky
Alexey
Why would you need this?
Lucky
The example code of pony_up.
https://github.com/luckydonald/pony_up
Lucky
Permanent link to the luckydonald/pony_up project you mentioned. (?)
Lucky
Okey, trying sqlite file now...
But I get the following error, and have no clue where it comes from.
Traceback (most recent call last):
File "/path/to/Python/pony_up/virtualenv3.6.0.venv/lib/python3.6/site-packages/pony/orm/dbapiprovider.py", line 48, in wrap_dbapi_exceptions
try: return func(provider, *args, **kwargs)
File "/path/to/Python/pony_up/virtualenv3.6.0.venv/lib/python3.6/site-packages/pony/orm/dbapiprovider.py", line 239, in execute
if arguments is None: cursor.execute(sql)
sqlite3.OperationalError: near "(": syntax error
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/path/to/Python/pony_up/example/example.py", line 17, in <module>
db = migrate(bind_to_database, folder_path=migrations_folder, python_import=python_import)
File "/path/to/Python/pony_up/pony_up/do_update.py", line 191, in do_all_migrations
bind_database_function(db)
File "/path/to/Python/pony_up/example/example.py", line 7, in bind_to_database
db.generate_mapping(create_tables=True)
File "<string>", line 2, in generate_mapping
File "/path/to/Python/pony_up/virtualenv3.6.0.venv/lib/python3.6/site-packages/pony/utils/utils.py", line 58, in cut_traceback
return func(*args, **kwargs)
File "/path/to/Python/pony_up/virtualenv3.6.0.venv/lib/python3.6/site-packages/pony/orm/core.py", line 882, in generate_mapping
if create_tables: database.create_tables(check_tables)
File "<string>", line 2, in create_tables
File "/path/to/Python/pony_up/virtualenv3.6.0.venv/lib/python3.6/site-packages/pony/utils/utils.py", line 58, in cut_traceback
return func(*args, **kwargs)
File "<string>", line 2, in create_tables
File "/path/to/Python/pony_up/virtualenv3.6.0.venv/lib/python3.6/site-packages/pony/orm/core.py", line 413, in new_func
try: return func(*args, **kwargs)
File "/path/to/Python/pony_up/virtualenv3.6.0.venv/lib/python3.6/site-packages/pony/orm/core.py", line 940, in create_tables
database.schema.create_tables(database.provider, connection)
File "/path/to/Python/pony_up/virtualenv3.6.0.venv/lib/python3.6/site-packages/pony/orm/dbschema.py", line 56, in create_tables
if name is None: db_object.create(provider, connection)
File "/path/to/Python/pony_up/virtualenv3.6.0.venv/lib/python3.6/site-packages/pony/orm/dbschema.py", line 84, in create
provider.execute(cursor, sql)
File "<string>", line 2, in execute
File "/path/to/Python/pony_up/virtualenv3.6.0.venv/lib/python3.6/site-packages/pony/orm/dbapiprovider.py", line 53, in wrap_dbapi_exceptions
except dbapi_module.OperationalError as e: raise OperationalError(e)
pony.orm.dbapiprovider.OperationalError: near "(": syntax error
Lucky
Wow, this is a lot of text 😓
Lucky
From the changelog (https://docs.ponyorm.com/upgrade_pony.html?highlight=structure#upgrade-pony-to-version-0-8)
"To move to a new version of Pony's we need to rename tables, and rename foreign_keys and index, because they contain the table name."
"Чтобы перейти на новую версию Пони нам нужно переименовать таблицы и также переименовать foreign_keys и index, потому что они содержат в себе имя таблицы."
Does that mean, I can't rename my Table later?
Or will official migrations be able to handle that?
Also is there somewhere to find about this official migrations?
Is that development done in the orm branch=
Amit
Hey everyone,
I'm Amit from India
Amit
How can I use PonyORM to translate natural language to sql query?
Rozen
wtf
Amit
Rozen
i'm not on thopic but that sounds like you should get your own code to proceess the text and have some logic plasmated into code that would do PonyORM queries
Amit
Rozen
i repeat, i'm not on thopic maybe you can do something more straightforward
Amit
Someone is actually did the same in python using nltk but I unable to build it on my local system
Lucky
Lucky
For reference, this is how I will block 2 processes to concourently try to do database migrations:
con = psycopg2.connect(host=POSTGRES_HOST, user=POSTGRES_USER, password=POSTGRES_PASSWORD, database=POSTGRES_DB)
cur = con.cursor()
cur.execute("SELECT pg_try_advisory_lock(85,80);") # update lock (ascii: UP)
res = cur.fetchone()
if not isinstance(res[0], bool) or not res[0]:
# True = success
# Fail => false or no bool
raise ValueError("Currently already upgrading. (Advisory Lock 85,80)")
# end if
db = migrate(...)
cur.execute("SELECT pg_advisory_unlock(85,80);") # update lock (ascii: UP)
res = cur.fetchone()
if not isinstance(res[0], bool) or not res[0]:
# True = success
# Fail => false or no bool
raise ValueError("Could not release update lock, lock was not held (Advisory Lock 85,80)")
# end if`
Following this article:
https://hashrocket.com/blog/posts/advisory-locks-in-postgres
Lucky
For reference, this is how I will block 2 processes to concourently try to do database migrations:
con = psycopg2.connect(host=POSTGRES_HOST, user=POSTGRES_USER, password=POSTGRES_PASSWORD, database=POSTGRES_DB)
cur = con.cursor()
cur.execute("SELECT pg_try_advisory_lock(85,80);") # update lock (ascii: UP)
res = cur.fetchone()
if not isinstance(res[0], bool) or not res[0]:
# True = success
# Fail => false or no bool
raise ValueError("Currently already upgrading. (Advisory Lock 85,80)")
# end if
db = migrate(...)
cur.execute("SELECT pg_advisory_unlock(85,80);") # update lock (ascii: UP)
res = cur.fetchone()
if not isinstance(res[0], bool) or not res[0]:
# True = success
# Fail => false or no bool
raise ValueError("Could not release update lock, lock was not held (Advisory Lock 85,80)")
# end if`
Following this article:
https://hashrocket.com/blog/posts/advisory-locks-in-postgres
However this only works in postgres.
How do you guys will do it?
Lucky
Also wrote about it here:
https://github.com/luckydonald/pony_up#my-application-with-the-migration-will-run-multible-times-at-the-same-time
Lucky
Permanent link to the luckydonald/pony_up project you mentioned. (?)
Henri
Hey!
I wanted to ask if there're still plans to integrate GraphQL in PonyORM.
Henri
I also wanted to introduce [more.pony](https://github.com/morepath/more.pony), which integrates PonyORM with [Morepath](http://morepath.readthedocs.io).
Henri
Released that last week.
Anonymous
let's say my table looks like this: servers: id|slots|...
Anonymous
Is there some easys way to sum the slots of all servers?
Anonymous
I already tried sth. like this
Anonymous
used_slots = db.Servers.sum(slots)
Anonymous
#got it :)
Anonymous
How to insert a value as type "NULL" using PONY?
Anonymous
if is_local:
ip_database = "NULL"
else:
ip_database = ip
Anonymous
This doesn't work, since the ip field in database is "varchar". So how to do this?
stsouko
atrr=None
Anonymous
How should I create my attribute inside the model then?
Anonymous
ip = (str, 32, nullable = True, default = None)?
Anonymous
got it
stsouko
ip = Optional(str)
Anonymous
ip = Optional(str, 32, nullable = True, default = None)
Anonymous
I use this atm. Using this PONY will handle all the stuff, using your solution will make the mysql server set the default value ;)
stsouko
without default = None
Anonymous
Why without?
stsouko
Optional atrrs if not set is None
Anonymous
So you mean, for all kind of optional attributes if NO value is net (None) then this will lead to type NULL in database automatic?
stsouko
Optional(str, 32, nullable=True)
Micaiah
Why doesn't Pony use interactive mode in iPython?
Micaiah
Oh nevermind, its when you use any form of python -i models.py
Alexey
Henri
Great! I think about GraphQL integration in Morepath and if PonyORM would support GraphQL this would be a good starting point. ☺
Kristian
Hi folks,
thanks a lot for this awesome ORM, it's really fun working with it!
I would like to be able to store decimal.Decimal in JSON Fields of pony entities. As the default JSON Encoder (json.JSONEncoder) does not support Decimal, I would like to be able to define the JSON-Encoder pony uses to write json fields to the database.
And for the return-ticket: I would like to specify the object_hook parameter of the json.JSONDecoder-Object used to read the JSON code back from the DB.
Is this possible?
Thank you!
Alexander
Hi Kristian! How you want to represent Decimal values in JSON fields?
Kristian
This depends on the use case. Probably as (specially marked?) string or float. (Which means of course, that during reading, all floats will be casted to decimals.)