+
Skip to content

fix create method for non-integer pk #119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions orm/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,6 @@ async def first(self, **kwargs):
return rows[0]

async def create(self, **kwargs):
# Validate the keyword arguments.
fields = self.model_cls.fields
validator = typesystem.Schema(
fields={key: value.validator for key, value in fields.items()}
Expand All @@ -393,13 +392,14 @@ async def create(self, **kwargs):
if value.validator.read_only and value.validator.has_default():
kwargs[key] = value.validator.get_default_value()

# Build the insert expression.
expr = self.table.insert()
expr = expr.values(**kwargs)

# Execute the insert, and return a new model instance.
instance = self.model_cls(**kwargs)
instance.pk = await self.database.execute(expr)
expr = self.table.insert().values(**kwargs)

if self.pkname not in kwargs:
instance.pk = await self.database.execute(expr)
else:
await self.database.execute(expr)

return instance

async def delete(self) -> None:
Expand Down
10 changes: 5 additions & 5 deletions tests/test_columns.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ async def rollback_transactions():


async def test_model_crud():
await Product.objects.create()
product = await Product.objects.create()

product = await Product.objects.get()
product = await Product.objects.get(pk=product.pk)
assert product.created.year == datetime.datetime.now().year
assert product.created_day == datetime.date.today()
assert product.data == {}
Expand All @@ -92,6 +92,6 @@ async def test_model_crud():
assert product.price == decimal.Decimal("999.99")
assert product.uuid == uuid.UUID("01175cde-c18f-4a13-a492-21bd9e1cb01b")

await User.objects.create(name="Chris")
user = await User.objects.get(name="Chris")
assert user.name == "Chris"
user = await User.objects.create(name="Chris")
assert isinstance(user.pk, uuid.UUID)
assert await User.objects.get(pk=user.pk) == user
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载