@@ -34,6 +34,12 @@ class TableMeta:
3434 non_default_columns : t .List [Column ] = field (default_factory = list )
3535 db : t .Optional [Engine ] = engine_finder ()
3636
37+ def get_column_by_name (self , name : str ) -> Column :
38+ column = [i for i in self .columns if i ._meta .name == name ]
39+ if len (column ) != 1 :
40+ raise ValueError (f"No matching column found with name == { name } " )
41+ return column [0 ]
42+
3743
3844class TableMetaclass (type ):
3945 def __str__ (cls ):
@@ -50,7 +56,9 @@ def __str__(cls):
5056 f"{ col ._meta .name } = ForeignKey({ col ._foreign_key_meta .references .__name__ } )"
5157 )
5258 else :
53- columns .append (f"{ col ._meta .name } = { col .__class__ .__name__ } ()" )
59+ columns .append (
60+ f"{ col ._meta .name } = { col .__class__ .__name__ } ()"
61+ )
5462 columns_string = spacer .join (columns )
5563 return f"class { cls .__name__ } (Table):\n " f" { columns_string } \n "
5664
@@ -88,7 +96,8 @@ def __init_subclass__(
8896 non_default_columns .append (column )
8997
9098 column ._meta ._name = attribute_name
91- column ._meta ._table = cls
99+ # Mypy wrongly thinks cls is a Table instance:
100+ column ._meta ._table = cls # type: ignore
92101
93102 db = db if db else engine_finder ()
94103
@@ -110,7 +119,9 @@ def __init__(self, **kwargs):
110119 value = column .default () if is_callable else column .default
111120 else :
112121 if not column ._meta .null :
113- raise ValueError (f"{ column ._meta .name } wasn't provided" )
122+ raise ValueError (
123+ f"{ column ._meta .name } wasn't provided"
124+ )
114125 self [column ._meta .name ] = value
115126
116127 unrecognized = kwargs .keys ()
@@ -128,11 +139,12 @@ def save(self) -> t.Union[Insert, Update]:
128139
129140 if type (self .id ) == int :
130141 # pre-existing row
131- kwargs = {
132- i : getattr (self , i ._meta .name , None ) for i in cls ._meta .columns
142+ kwargs : t .Dict [Column , t .Any ] = {
143+ i : getattr (self , i ._meta .name , None )
144+ for i in cls ._meta .columns
145+ if i ._meta .name != "id"
133146 }
134- _id = kwargs .pop ("id" )
135- return cls .update ().values (kwargs ).where (cls .id == _id )
147+ return cls .update ().values (kwargs ).where (cls .id == self .id )
136148 else :
137149 return cls .insert ().add (self )
138150
@@ -146,7 +158,7 @@ def remove(self) -> Delete:
146158 if type (_id ) != int :
147159 raise ValueError ("Can only delete pre-existing rows with an id." )
148160
149- self .id = None
161+ self .id = None # type: ignore
150162
151163 return self .__class__ .delete ().where (self .__class__ .id == _id )
152164
@@ -159,7 +171,9 @@ def get_related(self, column_name: str) -> Objects:
159171 foreign_key = cls .get_column_by_name (column_name )
160172
161173 if isinstance (foreign_key , ForeignKey ):
162- references : t .Type [Table ] = foreign_key ._foreign_key_meta .references
174+ references : t .Type [
175+ Table
176+ ] = foreign_key ._foreign_key_meta .references
163177
164178 return (
165179 references .objects ()
@@ -298,7 +312,10 @@ def create_without_columns(cls) -> Raw:
298312
299313 await Band.create().run()
300314 """
301- return Raw (table = cls , base = f'CREATE TABLE "{ cls ._meta .tablename } "()' )
315+ return Raw (
316+ table = cls ,
317+ base = QueryString (f'CREATE TABLE "{ cls ._meta .tablename } "()' ),
318+ )
302319
303320 @classmethod
304321 def drop (cls ) -> Drop :
0 commit comments