Cannot register custom range type using register_range
tmiyamon opened this issue · comments
Takuya Miyamoto commented
Please complete the following information:
- OS: Linux
- Psycopg version: 2.9.5
- Python version: 3.11
- PostgreSQL version: 11.22
- pip version: 23.3.1
Describe the bug
Please let us know:
1: what you did
I registered my custom range type using register_range with flask-sqlalchemy
2: what you expected to happen
I expected my custom range type will be registered correctly.
3: what happened instead
I got the following error.
File "/usr/local/app/app.py", line 22, in <module>
TimeRange.register(engine)
File "/usr/local/app/app.py", line 11, in register
register_range('timerange', cls, conn, globally=True)
File "/home/user/.cache/pypoetry/virtualenvs/wifishare-pyapi-woE4c0CP-py3.11/lib/python3.11/site-packages/psycopg2/_range.py", line 232, in register_range
caster = RangeCaster._from_db(pgrange, pyrange, conn_or_curs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/user/.cache/pypoetry/virtualenvs/wifishare-pyapi-woE4c0CP-py3.11/lib/python3.11/site-packages/psycopg2/_range.py", line 351, in _from_db
if conn.info.server_version < 90200:
^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'dict' object has no attribute 'server_version'
If possible, provide a script reproducing the issue.
from psycopg2.extras import Range
from psycopg2.extras import register_range
class TimeRange(Range):
@classmethod
def register(cls, engine):
conn = engine.raw_connection()
register_range('timerange', cls, conn, globally=True)
conn.close()
db = SQLAlchemy(session_options={})
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://username:password@localhost:port/DBNAME'
db.init_app(app)
engine = db.get_engine(app)
TimeRange.register(engine)
flask: 2.0.0
flask-sqlalchemy: 2.5.0
sqlalchemy: 1.4.52
Daniele Varrazzo commented
If conn.info
is a dict then conn
is not a psycopg2 connection. I don't know what engine.raw_connection()
returns, it's something in SQLAlchemy.