Ax: Postgres fails to create empty tables

Created on 25 Mar 2020  路  8Comments  路  Source: facebook/Ax

Cannot init Postgres+psycopg2 database, the codes are basically from docs:

init_engine_and_session_factory(url=db_url)
engine = get_engine()
create_all_tables(engine)

Used latest official docker for Postgres (https://hub.docker.com/_/postgres).

My error:

ProgrammingError: (psycopg2.errors.SyntaxError) type modifier is not allowed for type "text"
LINE 4:  data_json TEXT(4294967295) NOT NULL, 
                   ^

[SQL: 
CREATE TABLE data_v2 (
    id SERIAL NOT NULL, 
    data_json TEXT(4294967295) NOT NULL, 
    description VARCHAR(255), 
    experiment_id INTEGER, 
    time_created BIGINT NOT NULL, 
    trial_index INTEGER, 
    generation_strategy_id INTEGER, 
    PRIMARY KEY (id), 
    FOREIGN KEY(experiment_id) REFERENCES experiment_v2 (id), 
    FOREIGN KEY(generation_strategy_id) REFERENCES generation_strategy (id)
)

]

Looks like Text doesn't accept a length argument with PostgreSQL.
https://github.com/facebook/Ax/blob/eb707b168a4e66bc14ac72ba4bbdbc3c69387020/ax/storage/sqa_store/sqa_classes.py#L238

However, same works perfect with with SQLite.

bug wontfix

Most helpful comment

It is probably worth mentioning for whoever is bumping into this that another option to make Ax work on Postgres without touching the code locally or switching DB backend altogether, as suggested in https://github.com/sqlalchemy/sqlalchemy/issues/4443 mentioned above by @Jakepodell, is to alter the sqlalchemy compilation rules for the postgres dialect for the TEXT type, directly on client side, or on the code that is supposed to do the initial migration. For example this seems to work fine, tables are created on Postgres and the experiments are logged correctly:

from sqlalchemy import Text
from sqlalchemy.ext.compiler import compiles
from sqlalchemy.dialects import postgresql
from ax.storage.sqa_store.db import get_engine, create_all_tables

@compiles(Text, "postgresql")
def postgresql_text(type_, compiler, **kw):
    return "TEXT"

init_engine_and_session_factory(url="postgresql+psycopg2://user:pw@my_host:5432/test_db")

engine = get_engine()
create_all_tables(engine)
...

All 8 comments

Hi AlexJoz,

It looks like TEXT is of unlimited length with PostgreSQL, have you tried not including the length argument?

Hi AlexJoz,

It looks like TEXT is of unlimited length with PostgreSQL, have you tried not including the length argument?

No, I haven't changed Ax source code yet

okay, we're looking into this right now and will have an answer for you shortly!

So unfortunately this is a known issue with SQLAlchemy https://github.com/sqlalchemy/sqlalchemy/issues/4443. We're looking into a fix but it will likely take some time before we have Postgres compatibility :/. In the meantime, your options would be to move away from Postgres or to do a local checkout and modify the source code.

Let me know if there is anything else we can help with!

Thanks @Jakepodell !

Actually gonna keep this open, since hopefully we can fix and I want to keep the issue here as a reminder.

It is probably worth mentioning for whoever is bumping into this that another option to make Ax work on Postgres without touching the code locally or switching DB backend altogether, as suggested in https://github.com/sqlalchemy/sqlalchemy/issues/4443 mentioned above by @Jakepodell, is to alter the sqlalchemy compilation rules for the postgres dialect for the TEXT type, directly on client side, or on the code that is supposed to do the initial migration. For example this seems to work fine, tables are created on Postgres and the experiments are logged correctly:

from sqlalchemy import Text
from sqlalchemy.ext.compiler import compiles
from sqlalchemy.dialects import postgresql
from ax.storage.sqa_store.db import get_engine, create_all_tables

@compiles(Text, "postgresql")
def postgresql_text(type_, compiler, **kw):
    return "TEXT"

init_engine_and_session_factory(url="postgresql+psycopg2://user:pw@my_host:5432/test_db")

engine = get_engine()
create_all_tables(engine)
...

Thank you @lucapertile !

We'll close this issue for now unless it's reported by more users as there seems to be a solid workaround and we have not had a chance to attend to this in a year. Please feel free to follow up or reopen if you are effected by this still!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

arvieFrydenlund picture arvieFrydenlund  路  5Comments

ugurmengilli picture ugurmengilli  路  3Comments

ksanjeevan picture ksanjeevan  路  3Comments

FelixNeutatz picture FelixNeutatz  路  4Comments

newtonle picture newtonle  路  3Comments