How to use db.relationship in db.ForeignKey with Flask-SqlAlchemy - flask

Is db.relationship('Model_name') necessary to use with db.Foreignkey() in flask-sqlalchemy to specify a foreign key relationship without backref.

Related

Django: Force create index on ForeignKey fields when db's `supports_foreign_keys=False`

I'm trying to use Django (v4.1.3) with Planet Scale's DB which is MySQL compliant with the relevent exception of not supporting foreign keys.
For this, they provide a Django DB driver which subs class the MySQL DB feature class
from django.db.backends.mysql.features import \
DatabaseFeatures as MysqlBaseDatabaseFeatures
class DatabaseFeatures(MysqlBaseDatabaseFeatures):
supports_foreign_keys = False
Given this, when I generate migrations and apply them on my local MySQL server, I don't see any foreign keys being created as expected. However, I also don't see keys being created but I still want the keys.
For example with the MySQL driver, when I run SHOW CREATE app_user_groups (from django.contrib.auth package) I see the following key:
CREATE TABLE `auth_group_permissions` (
...
KEY `auth_group_permissio_permission_id_84c5c92e_fk_auth_perm` (`permission_id`),
)
But with the Planet Scale driver (supports_foreign_keys = False), Django doesn't create an key/index.
I know in my models, I can explicitly define keys but I don't find this ideal. In addition, I don't have control over models from other Django apps like django.contrib.auth and many other 3rd party ones.
What can I do to make Django create keys on django.db.models.ForeignKey despite support for foreign keys being off.

How to update primary key of an existing object in Django (with PostgreSQL)?

I need to change some objects primary keys in my django app, I wonder how can I achieve that?
Also my model has relations with other models.
Technically, it should be possible with an update query on the queryset:
MyModel.objects.filter(id=old_id).update(id=new_id)
The relations should cascade too if the constraints in the database have been set up correctly, but in general, I'd avoid updating PKs.

Django foreign key constraint with Model that lives in different database

I'm trying to use the oauth2_provider library which provides a model for AccessToken, which foreign keys into a User model. My User model will actually live in a different database from the OAuth2 token models. I can use a router to direct which DB to use for a particular model, but am I correct in concluding that Django would not support a foreign key to a model in a different DB?
If I still wanted to extend from the AbstractAccessToken with my User in a different DB, is there any way that Django allows me to populate the user_id foreign key column at all? Or would I simply need to leave it null and define and have my custom AccessToken class define its own unconstrained external_user_id column?
Django doesn't support any ForeignKey operations that span multiple databases. So, as you suggested, I think the best you can do is to provide your own IntegerField for the user and use it manually. Unfortunately that may require a lot of fiddling with that third-party library if it has a lot of internal code that's expecting to pull the user from the database.

Django model wih a ForeignKey pointing to a salesforce model

I am using django-salesforce and I would like to create a model within Django that has a ForeignKey field pointing to a SFDC model (hosted on force.com).
I created a custom model on force.com, let us call it SFModel, and I can successfully work on it from django (CRUD) by subclassing salesforce.models.Model.
I also created a django.db.models.Model, let us call it DJModel, that has a unique field ForeignKey(SFModel). This model is registered on the admin panel.
All models validate and I can go to my admin panel to try to create a new instance of DJModel. However, when I try to display the create_form in the admin I get the following error :
hasattr(): attribute name must be string
and the debug stream says
So I tried to set an arbitrary alias to the SF entry in the DATABASES of my settings.py. There is a dedicated variable for that :
SALESFORCE_DB_ALIAS = 'youralias'
But I still have the same problem.
Any recommendation?
Django doesn't support it and an external reference to Salesforce should be currently saved as a CharField and a reference to other databases as IntegerField.
Django docs about Limitations of multiple databases:
Django doesn’t currently provide any support for foreign key or many-to-many relationships spanning multiple databases. If you have used a router to partition models to different databases, any foreign key and many-to-many relationships defined by those models must be internal to a single database.
I tried the cross reference with sqlite as 'default' database. It was possible to create an object of model DJModel with cross-database reference from sqlite to Salesforce. It behaves similarly to normal Django cross-database references, without obscure errors and only a dot reference can be used.
EDIT: Simplified after many years.

ServiceStack OrmLite + Foreign Key

I've got the asp.net forms authentication tables in place, and I'd like to create a FK to one of the tables.
Is this possible without creating a type to be used with the attribute?
In the end, I created another skelton poco class that matched the table, this enabled me to place the foreign key.