xnuinside / omymodels

O!My Models (omymodels) is a library to generate Pydantic, Dataclasses, GinoORM Models, SqlAlchemy ORM, SqlAlchemy Core Table, Models from SQL DDL. And convert one models to another.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Syntax Error arises when NOT NULL and FOREIGN KEY exist in the same column

atsukiwi opened this issue · comments

Describe the bug
When a column is set as NOT NULL and a foreign key is added, Positional argument cannot appear after keyword arguments error arises in models.py.

To Reproduce
Steps to reproduce the behavior:

Create database.sql.

CREATE TABLE "merchants" (
  "id" int PRIMARY KEY,
  "merchant_name" varchar
);

CREATE TABLE "products" (
  "id" int PRIMARY KEY,
  "merchant_id" int NOT NULL
);

ALTER TABLE "products" ADD FOREIGN KEY ("merchant_id") REFERENCES "merchants" ("id");

Following models.py is produced after create_models.

import sqlalchemy as sa
from sqlalchemy.ext.declarative import declarative_base


Base = declarative_base()


class Merchants(Base):

    __tablename__ = 'merchants'

    id = sa.Column(sa.Integer(), primary_key=True)
    merchant_name = sa.Column(sa.String())


class Products(Base):

    __tablename__ = 'products'

    id = sa.Column(sa.Integer(), primary_key=True)
    merchant_id = sa.Column(sa.Integer(), nullable=False, sa.ForeignKey('merchants.id'))

Expected behavior
In models.py, the last sentence should be like following.

merchant_id = sa.Column(sa.Integer(), sa.ForeignKey('merchants.id'), nullable=False)

@atsukiwi hi! thanks for reporting the issue, I will work on fix for it

sorry that it is take so much time - fix was released in version 0.12.0 - https://pypi.org/project/omymodels/
tests was added - https://github.com/xnuinside/omymodels/blob/main/tests/functional/generator/test_sqlalchemy_core.py#L122

if will be any new issues - feel free to open the new ticket