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

SQLModel codegen produces syntax errors #302

Open
2 tasks done
NixBiks opened this issue Nov 16, 2023 · 6 comments
Open
2 tasks done

SQLModel codegen produces syntax errors #302

NixBiks opened this issue Nov 16, 2023 · 6 comments
Labels

Comments

@NixBiks
Copy link

NixBiks commented Nov 16, 2023

Things to check first

  • I have searched the existing issues and didn't find my bug already reported there

  • I have checked that my bug is still present in the latest release

Sqlacodegen version

3.0.0rc3

SQLAlchemy version

2.0.23

RDBMS vendor

PostgreSQL

What happened?

The screenshot shows some of the issues.

  1. There are unused imports (can easily be fixed by running ruff after codegen)
  2. Missing imports, i.e. mapped_column.
  3. Syntax error due to a , in line 22.
Screenshot 2023-11-16 at 09 18 05

Database schema for reproducing the bug

CREATE TABLE
  logs (
    timestamp timestamp NOT NULL,
    level text NOT NULL,
    message text NOT NULL,
    schedule_id bigint REFERENCES schedules (id),
    worker_id text,
    run_id text,
    id bigint GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY
  );

-- sort by timestamp
CREATE INDEX logs_timestamp_desc_idx ON logs (timestamp DESC);

-- create view of logs
CREATE VIEW logs_view AS
  SELECT
    timestamp,
    level,
    message,
    schedule_id,
    worker_id,
    run_id
  FROM logs
  ORDER BY timestamp DESC;
@NixBiks NixBiks added the bug label Nov 16, 2023
@madsenwattiq
Copy link

I see this as well. In order to fix broken code, I had to manually add the "metadata" variable to all association tables that didn't have primary keys - that's the bug with the double commas. After that, it works in 3.0.0rc3.

@agronholm
Copy link
Owner

It's not supposed to use mapped_column at all. All the tests expect that it uses Column() and not mapped_column(). The latter is a thing only on SQLAlchemy 2. Before sqlmodel 0.0.12 (released 7 hours ago), it didn't even work with SQLAlchemy 2 at all.

@agronholm
Copy link
Owner

I'm pretty sure that this was caused by `sqlmodel now supporting SQLAlchemy 2, and sqlacodegen wasn't ready for that.

@michelmetran
Copy link

I'm pretty sure that this was caused by `sqlmodel now supporting SQLAlchemy 2, and sqlacodegen wasn't ready for that.

Thank you for the information!

After that, I used an environment with SQLAlchemy < 2 and it worked!

conda create --name sqlacodegen-py39 -c conda-forge python=3.9 sqlacodegen==3.0.0rc3 sqlalchemy==1.4.51 pymssql==2.2.7

Now the mapped_column no longer appears, as it is not yet compatible with SQLModel

@yaoguoba
Copy link

This issue still exists in rc5, which still includes mapped_column. I replaced mapped_column with sqlmodel.Column, You can work normally now.

1 similar comment
@yaoguoba
Copy link

This issue still exists in rc5, which still includes mapped_column. I replaced mapped_column with sqlmodel.Column, You can work normally now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants