simplified declarative mapping code, eliminated use of datatype classes like sqlalchemy.Text, thus eliminated a naming conflict between two classes named "Text"

This commit is contained in:
eclipse 2025-07-18 10:31:24 +02:00
parent 4b951ebf7d
commit 270085e424
3 changed files with 31 additions and 31 deletions

View File

@ -1,8 +1,8 @@
# based on output of `sqlacodegen --generator declarative sqlite:///path/to/the_works.sqlite` # code is based on output from `sqlacodegen --generator declarative sqlite:///path/to/the_works.sqlite`
from typing import List, Optional from typing import List, Optional
from sqlalchemy import Column, ForeignKey, Integer, LargeBinary, Table, Text, types from sqlalchemy import Column, ForeignKey, Table, types
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship
from flask import url_for from flask import url_for
@ -27,17 +27,17 @@ class Base(DeclarativeBase):
class Genre(Base): class Genre(Base):
__tablename__ = 'Genre' __tablename__ = 'Genre'
ID: Mapped[int] = mapped_column(Integer, primary_key=True) ID: Mapped[int] = mapped_column(primary_key=True)
Genre: Mapped[str] Genre: Mapped[str]
text: Mapped[List['Text_']] = relationship('Text_', secondary='Text_Genre', back_populates='genre') text: Mapped[List['Text']] = relationship('Text', secondary='Text_Genre', back_populates='genre')
werk: Mapped[List['Werk']] = relationship('Werk', secondary='Werk_Genre', back_populates='genre') werk: Mapped[List['Werk']] = relationship('Werk', secondary='Werk_Genre', back_populates='genre')
class Herausgeber(Base): class Herausgeber(Base):
__tablename__ = 'Herausgeber' __tablename__ = 'Herausgeber'
ID: Mapped[int] = mapped_column(Integer, primary_key=True) ID: Mapped[int] = mapped_column(primary_key=True)
Name: Mapped[str] Name: Mapped[str]
werk: Mapped[List['Werk']] = relationship('Werk', secondary='Werk_Herausgeber', back_populates='herausgeber') werk: Mapped[List['Werk']] = relationship('Werk', secondary='Werk_Herausgeber', back_populates='herausgeber')
@ -46,7 +46,7 @@ class Herausgeber(Base):
class Pseudonym(Base): class Pseudonym(Base):
__tablename__ = 'Pseudonym' __tablename__ = 'Pseudonym'
ID: Mapped[int] = mapped_column(Integer, primary_key=True) ID: Mapped[int] = mapped_column(primary_key=True)
Pseudonym: Mapped[str] Pseudonym: Mapped[str]
veroeffentlichung: Mapped[List['Veroeffentlichung']] = relationship('Veroeffentlichung', back_populates='pseudonym') veroeffentlichung: Mapped[List['Veroeffentlichung']] = relationship('Veroeffentlichung', back_populates='pseudonym')
@ -55,25 +55,25 @@ class Pseudonym(Base):
class Sprache(Base): class Sprache(Base):
__tablename__ = 'Sprache' __tablename__ = 'Sprache'
ID: Mapped[int] = mapped_column(Integer, primary_key=True) ID: Mapped[int] = mapped_column(primary_key=True)
Sprache: Mapped[str] Sprache: Mapped[str]
text: Mapped[List['Text_']] = relationship('Text_', back_populates='sprache') text: Mapped[List['Text']] = relationship('Text', back_populates='sprache')
class Textform(Base): class Textform(Base):
__tablename__ = 'Textform' __tablename__ = 'Textform'
ID: Mapped[int] = mapped_column(Integer, primary_key=True) ID: Mapped[int] = mapped_column(primary_key=True)
Textform: Mapped[str] Textform: Mapped[str]
text: Mapped[List['Text_']] = relationship('Text_', back_populates='textform') text: Mapped[List['Text']] = relationship('Text', back_populates='textform')
class Titelbild(Base): class Titelbild(Base):
__tablename__ = 'Titelbild' __tablename__ = 'Titelbild'
ID: Mapped[int] = mapped_column(Integer, primary_key=True) ID: Mapped[int] = mapped_column(primary_key=True)
Mimetype: Mapped[str] Mimetype: Mapped[str]
Dateiname: Mapped[str] Dateiname: Mapped[str]
Dateigroesse: Mapped[int] Dateigroesse: Mapped[int]
@ -81,7 +81,7 @@ class Titelbild(Base):
Hoehe: Mapped[int] Hoehe: Mapped[int]
Bild: Mapped[bytes] Bild: Mapped[bytes]
Thumbnail: Mapped[bytes] Thumbnail: Mapped[bytes]
sha256: Mapped[str] = mapped_column(Text, unique=True) sha256: Mapped[str] = mapped_column(unique=True)
werk: Mapped[List['Werk']] = relationship('Werk', back_populates='titelbild') werk: Mapped[List['Werk']] = relationship('Werk', back_populates='titelbild')
@ -95,7 +95,7 @@ class Titelbild(Base):
class Verlag(Base): class Verlag(Base):
__tablename__ = 'Verlag' __tablename__ = 'Verlag'
ID: Mapped[int] = mapped_column(Integer, primary_key=True) ID: Mapped[int] = mapped_column(primary_key=True)
Verlag: Mapped[str] Verlag: Mapped[str]
reihe: Mapped[List['Reihe']] = relationship('Reihe', back_populates='verlag') reihe: Mapped[List['Reihe']] = relationship('Reihe', back_populates='verlag')
@ -105,7 +105,7 @@ class Verlag(Base):
class Werksform(Base): class Werksform(Base):
__tablename__ = 'Werksform' __tablename__ = 'Werksform'
ID: Mapped[int] = mapped_column(Integer, primary_key=True) ID: Mapped[int] = mapped_column(primary_key=True)
Werksform: Mapped[str] Werksform: Mapped[str]
werk: Mapped[List['Werk']] = relationship('Werk', back_populates='werksform') werk: Mapped[List['Werk']] = relationship('Werk', back_populates='werksform')
@ -114,19 +114,19 @@ class Werksform(Base):
class Reihe(Base): class Reihe(Base):
__tablename__ = 'Reihe' __tablename__ = 'Reihe'
ID: Mapped[int] = mapped_column(Integer, primary_key=True) ID: Mapped[int] = mapped_column(primary_key=True)
Titel: Mapped[str] Titel: Mapped[str]
Verlag: Mapped[Optional[str]] = mapped_column('Verlag', ForeignKey('Verlag.ID')) Verlag: Mapped[Optional[str]] = mapped_column('Verlag', ForeignKey('Verlag.ID'))
verlag: Mapped[Optional['Verlag']] = relationship('Verlag', back_populates='reihe') verlag: Mapped[Optional['Verlag']] = relationship('Verlag', back_populates='reihe')
text: Mapped[List['Text_']] = relationship('Text_', back_populates='reihe') text: Mapped[List['Text']] = relationship('Text', back_populates='reihe')
werk: Mapped[List['Werk']] = relationship('Werk', back_populates='reihe') werk: Mapped[List['Werk']] = relationship('Werk', back_populates='reihe')
class Text_(Base): class Text(Base):
__tablename__ = 'Text' __tablename__ = 'Text'
ID: Mapped[int] = mapped_column(Integer, primary_key=True) ID: Mapped[int] = mapped_column(primary_key=True)
Titel: Mapped[str] Titel: Mapped[str]
Untertitel: Mapped[Optional[str]] Untertitel: Mapped[Optional[str]]
Reihe: Mapped[Optional[int]] = mapped_column('Reihe', ForeignKey('Reihe.ID')) Reihe: Mapped[Optional[int]] = mapped_column('Reihe', ForeignKey('Reihe.ID'))
@ -143,7 +143,7 @@ class Text_(Base):
class Werk(Base): class Werk(Base):
__tablename__ = 'Werk' __tablename__ = 'Werk'
ID: Mapped[int] = mapped_column(Integer, primary_key=True) ID: Mapped[int] = mapped_column(primary_key=True)
Titel: Mapped[str] Titel: Mapped[str]
Untertitel: Mapped[Optional[str]] Untertitel: Mapped[Optional[str]]
Werksform: Mapped[Optional[int]] = mapped_column('Werksform', ForeignKey('Werksform.ID')) Werksform: Mapped[Optional[int]] = mapped_column('Werksform', ForeignKey('Werksform.ID'))
@ -168,18 +168,10 @@ class Werk(Base):
veroeffentlichung: Mapped[List['Veroeffentlichung']] = relationship('Veroeffentlichung', back_populates='werk') veroeffentlichung: Mapped[List['Veroeffentlichung']] = relationship('Veroeffentlichung', back_populates='werk')
t_Text_Genre = Table(
'Text_Genre',
Base.metadata,
Column('Text', ForeignKey('Text.ID'), primary_key=True),
Column('Genre', ForeignKey('Genre.ID'), primary_key=True)
)
class Veroeffentlichung(Base): class Veroeffentlichung(Base):
__tablename__ = 'Veroeffentlichung' __tablename__ = 'Veroeffentlichung'
ID: Mapped[int] = mapped_column(Integer, primary_key=True) ID: Mapped[int] = mapped_column(primary_key=True)
Text: Mapped[int] = mapped_column('Text', ForeignKey('Text.ID')) Text: Mapped[int] = mapped_column('Text', ForeignKey('Text.ID'))
Werk: Mapped[int] = mapped_column('Werk', ForeignKey('Werk.ID')) Werk: Mapped[int] = mapped_column('Werk', ForeignKey('Werk.ID'))
Pseudonym: Mapped[int] = mapped_column('Pseudonym', ForeignKey('Pseudonym.ID')) Pseudonym: Mapped[int] = mapped_column('Pseudonym', ForeignKey('Pseudonym.ID'))
@ -187,10 +179,18 @@ class Veroeffentlichung(Base):
AltUntertitel: Mapped[Optional[str]] AltUntertitel: Mapped[Optional[str]]
pseudonym: Mapped['Pseudonym'] = relationship('Pseudonym', back_populates='veroeffentlichung') pseudonym: Mapped['Pseudonym'] = relationship('Pseudonym', back_populates='veroeffentlichung')
text: Mapped['Text_'] = relationship('Text_', back_populates='veroeffentlichung') text: Mapped['Text'] = relationship('Text', back_populates='veroeffentlichung')
werk: Mapped['Werk'] = relationship('Werk', back_populates='veroeffentlichung') werk: Mapped['Werk'] = relationship('Werk', back_populates='veroeffentlichung')
t_Text_Genre = Table(
'Text_Genre',
Base.metadata,
Column('Text', ForeignKey('Text.ID'), primary_key=True),
Column('Genre', ForeignKey('Genre.ID'), primary_key=True)
)
t_Werk_Genre = Table( t_Werk_Genre = Table(
'Werk_Genre', 'Werk_Genre',
Base.metadata, Base.metadata,

View File

@ -1,7 +1,7 @@
from flask import Blueprint, render_template, request, redirect, flash, url_for from flask import Blueprint, render_template, request, redirect, flash, url_for
from sqlalchemy import select, insert, update, delete from sqlalchemy import select, insert, update, delete
from the_works.database import db from the_works.database import db
from the_works.models import Text_ as Text, Reihe, Sprache, Textform, t_Text_Genre, Genre from the_works.models import Text, Reihe, Sprache, Textform, t_Text_Genre, Genre
bp = Blueprint("text", __name__) bp = Blueprint("text", __name__)

View File

@ -1,7 +1,7 @@
from flask import Blueprint, render_template, request, redirect, flash, url_for from flask import Blueprint, render_template, request, redirect, flash, url_for
from sqlalchemy import select, insert, update, delete from sqlalchemy import select, insert, update, delete
from the_works.database import db from the_works.database import db
from the_works.models import Veroeffentlichung, Text_ as Text, Werk, Werksform, Pseudonym from the_works.models import Veroeffentlichung, Text, Werk, Werksform, Pseudonym
bp = Blueprint("veroeffentlichung", __name__) bp = Blueprint("veroeffentlichung", __name__)