question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add OneToOne relation type

See original GitHub issue

apart from OneToMany (by ForeignKey) and ManyToMany add also support for OneToOne relations.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:3
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
pylakeycommented, Aug 22, 2021

Thanks, I’ll try and make a PR

0reactions
bstrdsmkrcommented, Nov 2, 2021

So before I saw your reply, I was looking through the source to see if this was something I could hack in and discovered that the ForeignKey function hardcodes primary_key=False which explains the error above. I monkey patched the field like this:

patched_field = ForeignKey(Files, related_name='contents')
patched_field.primary_key=True

class FileContents(Model):
    class Meta:
        tablename = 'file_contents'
        metadata = sqlalchemy.MetaData(schema='data_reception')
        database=database
    versioned_filename: Files = patched_field
    n_samples: int = Integer()

And that allowed things to continue and me to actually run the query. I’m not sure if this is an “expected” failure (given the impending compound pk work) or a sort of bug that would otherwise allow this kind of work around to work, so thought I’d report it. The query produces SQL like this:

SELECT data_reception.file_info.versioned_filename AS versioned_filename,
	   data_reception.file_info.md5_checksum AS md5_checksum,
	   qb6461_file_contents.versioned_filename AS qb6461_versioned_filename,
	   qb6461_file_contents.n_samples AS qb6461_n_samples
FROM data_reception.file_info
	LEFT OUTER JOIN data_reception.file_contents AS qb6461_file_contents
	ON qb6461_file_contents.versioned_filename=file_info.versioned_filename
WHERE data_reception.file_info.datastream IN ('sgpxsaprvptI6.a1')
AND data_reception.qb6461_file_contents.n_samples >= 500

resulting in an SQL error:

SQL Error [42P01]: ERROR: invalid reference to FROM-clause entry for table "qb6461_file_contents"
  Hint: There is an entry for table "qb6461_file_contents", but it cannot be referenced from this part of the query.

If I remove data_reception (the Postgres Schema) from the AND condition, the query executes as expected.

Is this a new bug with the schema being inappropriately inserted, or a known effect that will be covered by the compound pk work?

Read more comments on GitHub >

github_iconTop Results From Across the Web

One-to-one relations - typeorm - GitBook
One-to-one is a relation where A contains only one instance of B, and B contains only one instance of A. Let's take for...
Read more >
What Is a One-to-One Relationship in a Database?
A one-to-one relationship is a relationship where a record in one table is associated with exactly one record in another table. This type...
Read more >
Video: Create one-to-one relationships
Create a one-to-one relationship steps · Open a table. · In Design View, add a new field, select the Data Type value, and...
Read more >
One-to-One Relationship in JPA
One-to-one mappings can be of two types: optional and mandatory. So far, we've seen only mandatory relationships.
Read more >
One-to-one relationships
To define a one-to-one relationship, use OneToOneField . What follows are examples of operations that can be performed using the Python API facilities....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found