Skip to content

Commit 7a6c2ea

Browse files
author
Daniel Townsend
committed
fixing primary key defaults for postgres
1 parent 3e74063 commit 7a6c2ea

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

piccolo/columns/column_types.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,24 @@ def __init__(self, **kwargs) -> None:
3434
DEFAULT = Unquoted('DEFAULT')
3535
NULL = Unquoted('null')
3636

37+
3738
class PrimaryKey(Column):
3839
# Was column_type = 'SERIAL' for Postgres
3940
column_type = 'INTEGER'
4041

42+
def default(self, engine_type: str = 'postgres'):
43+
if engine_type == 'postgres':
44+
return DEFAULT
45+
elif engine_type == 'sqlite':
46+
return NULL
47+
else:
48+
raise Exception('Unrecognized engine type')
49+
4150
def __init__(self, **kwargs) -> None:
4251
kwargs.update({
4352
'primary': True,
4453
'key': True
4554
})
46-
# self.default = DEFAULT for Postgres
47-
self.default = NULL
4855
super().__init__(**kwargs)
4956

5057

piccolo/table.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,9 @@ def __init__(self, **kwargs):
199199
# Can't use inspect - can't tell that datetime.datetime.now
200200
# is a callable.
201201
is_callable = hasattr(column.default, '__call__')
202-
value = column.default() if is_callable else column.default
202+
value = column.default(
203+
engine_type=self.Meta.db.engine_type
204+
) if is_callable else column.default
203205
else:
204206
if not column.null:
205207
raise ValueError(f"{column._name} wasn't provided")

tests/test_alter.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ def test_rename(self):
1616

1717
rename_query.run_sync()
1818

19+
# The problem now is select * has changed
20+
# Need to use a raw select query instead ...
1921
select_query = Band.select
2022
response = select_query.run_sync()
2123

0 commit comments

Comments
 (0)