from typing import Optional, List
from sqlmodel import SQLModel, Field, Relationship


class SongBase(SQLModel):
    name: str
    artist: str
    year: Optional[int] = None


class Song(SongBase, table=True):
    id: int = Field(primary_key=True)
class SongRead(SongBase):
    id: int


class SongCreate(SongBase):
    pass

class Increment(SQLModel, table=True):
    id: int = Field(primary_key=True)


# #############################################################################
class ListingBase(SQLModel):
    url: str
    
class Listing(ListingBase, table=True):
    __tablename__ = 'listings'
    id: int = Field(primary_key=True)
    images: List["Image"] = Relationship(back_populates="listing", 
    sa_relationship_kwargs={'lazy': 'selectin'})
    
class ListingRead(ListingBase):
    id: str

# #############################################################################

class ImageBase(SQLModel):
    url: str
    size_x: float
    size_y: float
    listing_id: Optional[int] = Field(default=None, foreign_key="listings.id")

class Image(ImageBase, table=True):
    __tablename__ = 'images'
    id: int = Field(primary_key=True)
    listing: Optional[Listing] = Relationship(back_populates="images",
    sa_relationship_kwargs={'lazy': 'selectin'})

class ImageRead(ImageBase):
    id: int
class ImageReadWithListings(ImageRead):
    listing: Optional[Listing] = None

class ListingReadWithImages(ListingRead):
    images: List["ImageRead"] = []

sqlmodel.Relationship sqlmodel.Field
[((224, 247), 'sqlmodel.Field', 'Field', ([], {'primary_key': '(True)'}), '(primary_key=True)\n', (229, 247), False, 'from sqlmodel import SQLModel, Field, Relationship\n'), ((379, 402), 'sqlmodel.Field', 'Field', ([], {'primary_key': '(True)'}), '(primary_key=True)\n', (384, 402), False, 'from sqlmodel import SQLModel, Field, Relationship\n'), ((617, 640), 'sqlmodel.Field', 'Field', ([], {'primary_key': '(True)'}), '(primary_key=True)\n', (622, 640), False, 'from sqlmodel import SQLModel, Field, Relationship\n'), ((669, 756), 'sqlmodel.Relationship', 'Relationship', ([], {'back_populates': '"""listing"""', 'sa_relationship_kwargs': "{'lazy': 'selectin'}"}), "(back_populates='listing', sa_relationship_kwargs={'lazy':\n    'selectin'})\n", (681, 756), False, 'from sqlmodel import SQLModel, Field, Relationship\n'), ((997, 1043), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'foreign_key': '"""listings.id"""'}), "(default=None, foreign_key='listings.id')\n", (1002, 1043), False, 'from sqlmodel import SQLModel, Field, Relationship\n'), ((1124, 1147), 'sqlmodel.Field', 'Field', ([], {'primary_key': '(True)'}), '(primary_key=True)\n', (1129, 1147), False, 'from sqlmodel import SQLModel, Field, Relationship\n'), ((1181, 1267), 'sqlmodel.Relationship', 'Relationship', ([], {'back_populates': '"""images"""', 'sa_relationship_kwargs': "{'lazy': 'selectin'}"}), "(back_populates='images', sa_relationship_kwargs={'lazy':\n    'selectin'})\n", (1193, 1267), False, 'from sqlmodel import SQLModel, Field, Relationship\n')]