question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Postgres fails to create empty tables

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
lucapertilecommented, Apr 15, 2020

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)
...

2reactions
ldworkincommented, Mar 26, 2020

Thanks @Jakepodell !

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Create empty table in postgresql - Stack Overflow
You can have a table with no columns, and even with some rows in it: CREATE TABLE nocolumn (dummy INTEGER NOT NULL PRIMARY...
Read more >
CREATE TABLE IF ONLY NOT EMPTY RESULT SET
Check if a table is empty after creation and throw an exception if it is. Catch that exception (and do nothing). That rolls...
Read more >
Documentation: 15: CREATE TABLE - PostgreSQL
CREATE TABLE will create a new, initially empty table in the current database. The table will be owned by the user issuing the...
Read more >
PostgreSQL - CREATE Table - Tutorialspoint
You can verify if your table has been created successfully using \d command, which will be used to list down all the tables...
Read more >
PostgreSQL Create Table IF NOT EXISTS - Command Prompt
If a database already has a table with the same name, then a "relation already exists" error will appear in Postgres. To avoid...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found