Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support SERIAL column types #289

Open
Mause opened this issue Jun 23, 2022 · 3 comments · May be fixed by #866
Open

Support SERIAL column types #289

Mause opened this issue Jun 23, 2022 · 3 comments · May be fixed by #866
Labels
enhancement New feature or request

Comments

@Mause
Copy link
Owner

Mause commented Jun 23, 2022

Right now duckdb doesn't have native support for serial column types. There are a couple of different ways we could emulate support however

@Mause Mause added the enhancement New feature or request label Jun 23, 2022
@WoolenWang
Copy link

normal columns like this:

        id = sqlalchemy.Column(sqlalchemy.Integer, primary_key=True, autoincrement=True)

will get error ?

File "D:\tools\python37_x64\python37_x64\lib\site-packages\duckdb_engine_init_.py", line 87, in execute
self.c.execute(statement, parameters)
│ │ └ ()
│ └ '\nCREATE TABLE back_test_log (\n\tid SERIAL NOT NULL, \n\tdt TIMESTAMP WITHOUT TIME ZONE NOT NULL, \n\trun_dt TIMESTAMP WITHOUT...
└ <duckdb_engine.ConnectionWrapper object at 0x000002B8F4A3F248>
RuntimeError: Catalog Error: Type with name SERIAL does not exist!
Did you mean "string"?

@Mause
Copy link
Owner Author

Mause commented Aug 10, 2022

Also blocked by duckdb/duckdb#1768

@ml31415
Copy link

ml31415 commented Dec 21, 2023

As a temporary workaround, it is possible to hack into the sqlalchemy SQL generation to achieve this. What is required is to replace the following code as seen at duckdb/duckdb#1768 (comment)

CREATE TABLE integers(i SERIAL);
-- this statement is equivalent to
CREATE SEQUENCE __internal_serial_sequence_integers_1;
CREATE TABLE integers(i INTEGER DEFAULT(nextval('__internal_serial_sequence_integers_1')));

This code replaces the table generation at the necessary places:

from sqlalchemy.ext.compiler import compiles
from sqlalchemy.schema import CreateColumn, CreateTable

@compiles(CreateTable, "duckdb")
def prepend_sequence(element, compiler, **kw):
    """Prepend a table with an auto-incremented primary key with the necessary sequence."""
    table_sql = compiler.visit_create_table(element, **kw)
    table_name = element.element.name
    seq_name = f"__internal_serial_{table_name}"
    if seq_name not in table_sql:
        # no need to create a sequence
        return table_sql
    seq_sql = f"\nCREATE SEQUENCE {seq_name};"
    return seq_sql + table_sql


@compiles(CreateColumn, "duckdb")
def replace_serial_autoincrement(element, compiler, **kw):
    """Replace the first SERIAL field with a duckdb-style auto-incremented integer."""
    column_sql = compiler.visit_create_column(element, **kw)
    if "SERIAL" not in column_sql or not kw.get("first_pk"):
        return column_sql
    table_name = element.element.table.name
    autoinc_type = f"INTEGER DEFAULT(nextval('__internal_serial_{table_name}'))"
    return column_sql.replace("SERIAL", autoinc_type)

This fails if there are multiple SERIALs in one table, otherwise it should be quite specific and hopefully shouldn't cause any troublesome side effects.

ml31415 added a commit to ml31415/duckdb_engine that referenced this issue Jan 6, 2024
@ml31415 ml31415 linked a pull request Jan 6, 2024 that will close this issue
ml31415 added a commit to ml31415/duckdb_engine that referenced this issue Jan 6, 2024
ml31415 added a commit to ml31415/duckdb_engine that referenced this issue Jan 8, 2024
ml31415 added a commit to ml31415/duckdb_engine that referenced this issue Feb 29, 2024
ml31415 added a commit to ml31415/duckdb_engine that referenced this issue Apr 30, 2024
ml31415 added a commit to ml31415/duckdb_engine that referenced this issue May 25, 2024
ml31415 added a commit to ml31415/duckdb_engine that referenced this issue Jun 5, 2024
ml31415 added a commit to ml31415/duckdb_engine that referenced this issue Jul 16, 2024
ml31415 added a commit to ml31415/duckdb_engine that referenced this issue Sep 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants