omymodels icon indicating copy to clipboard operation
omymodels copied to clipboard

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

Open atsukiwi opened this issue 3 years ago • 1 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 avatar Aug 04 '22 04:08 atsukiwi

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

xnuinside avatar Aug 06 '22 21:08 xnuinside

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

xnuinside avatar Oct 11 '22 21:10 xnuinside