Skip to content

Commit 59209d8

Browse files
committed
moved more attributes into ColumnMeta
1 parent 2cd3092 commit 59209d8

File tree

2 files changed

+16
-18
lines changed

2 files changed

+16
-18
lines changed

piccolo/columns/base.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@
2828

2929
@dataclass
3030
class ColumnMeta:
31+
# General attributes:
32+
null: bool = True
33+
primary: bool = False
34+
key: bool = False
35+
unique: bool = False
36+
3137
# Set by the Table Metaclass:
3238
_name: t.Optional[str] = None
3339
_table: t.Optional[Table] = None
@@ -95,11 +101,9 @@ def __init__(
95101
key: bool = False,
96102
unique: bool = False,
97103
) -> None:
98-
self.null = null
99-
self.primary = primary
100-
self.key = key
101-
self.unique = unique
102-
self._meta = ColumnMeta()
104+
self._meta = ColumnMeta(
105+
null=null, primary=primary, key=key, unique=unique
106+
)
103107

104108
def is_in(self, values: Iterable) -> Where:
105109
return Where(column=self, values=values, operator=In)
@@ -159,11 +163,11 @@ def querystring(self) -> QueryString:
159163
self, "column_type", self.__class__.__name__.upper()
160164
)
161165
query = f"{self._meta.name} {column_type}"
162-
if self.primary:
166+
if self._meta.primary:
163167
query += " PRIMARY"
164-
if self.key:
168+
if self._meta.key:
165169
query += " KEY"
166-
if self.unique:
170+
if self._meta.unique:
167171
query += " UNIQUE"
168172

169173
foreign_key_meta = getattr(self, "foreign_key_meta", None)

piccolo/table.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,7 @@ def __str__(cls):
9090
f"{col._meta.name} = ForeignKey({col._foreign_key_meta.references.__name__})"
9191
)
9292
else:
93-
columns.append(
94-
f"{col._meta.name} = {col.__class__.__name__}()"
95-
)
93+
columns.append(f"{col._meta.name} = {col.__class__.__name__}()")
9694
columns_string = spacer.join(columns)
9795
return f"class {cls.__name__}(Table):\n" f" {columns_string}\n"
9896

@@ -119,10 +117,8 @@ def __init__(self, **kwargs):
119117
is_callable = hasattr(column.default, "__call__")
120118
value = column.default() if is_callable else column.default
121119
else:
122-
if not column.null:
123-
raise ValueError(
124-
f"{column._meta.name} wasn't provided"
125-
)
120+
if not column._meta.null:
121+
raise ValueError(f"{column._meta.name} wasn't provided")
126122
self[column._meta.name] = value
127123

128124
unrecognized = kwargs.keys()
@@ -171,9 +167,7 @@ def get_related(self, column_name: str) -> Objects:
171167
foreign_key = cls.get_column_by_name(column_name)
172168

173169
if isinstance(foreign_key, ForeignKey):
174-
references: t.Type[
175-
Table
176-
] = foreign_key._foreign_key_meta.references
170+
references: t.Type[Table] = foreign_key._foreign_key_meta.references
177171

178172
return (
179173
references.objects()

0 commit comments

Comments
 (0)