web2py reference field unique=True attribute not working - python

I working with web2py and have the following table definition:
my_info = db.define_table('my_info',
Field('my_info_id', 'reference other_info', requires=IS_IN_DB(db, other_info.id, ''), unique=True),
Field('interface', 'string', length=32, requires=[IS_NOT_EMPTY()]),
Field('size', 'integer', requires=[IS_NOT_EMPTY()]))
For some reason when I view the mysql create table syntax I do not see the UNIQUE_KEY field set. Here is the create table syntax:
CREATE TABLE `mgmt_info` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`my_info_id` int(11) DEFAULT NULL,
`interface` varchar(32) DEFAULT NULL,
`size` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `id__idx` (`id`),
CONSTRAINT `my_info_ibfk_1` FOREIGN KEY (`my_info_id`) REFERENCES `other_info` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8
Hope someone can let me know why I am not able to set a foreign key to unique and why it does not work.
Thanks,
nav

It is odd, but odder is that in your unique field you did put the IS_IN_DB validator. I do not know what you wanted to do, but you should replace by IS_NOT_IN_DB validator. Therefore, it will assure to you that your field 'my_info_id' is unique in your database.

Related

need your help Mysql ERROR I tried but failed:

I am trying to run a app cloned from following repo
https://github.com/myogeshchavan97/fullstack_banking_app and stuck with mysql
I get following error when i run source /server/scripts.sql
I am using latest Server version: 8.0.23 Homebrew
ERROR 1064 (42000): You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near 'bank_account' at line 1
ERROR 1064 (42000): You
have an error in your SQL syntax; check the manual that corresponds
to your MySQL server version for the right syntax to use near
'BIGSERIAL PRIMARY KEY NOT NULL, first_name VARCHAR(32) NOT NULL,
last_name V' at line 2
ERROR 1064 (42000): You have an error in your
SQL syntax; check the manual that corresponds to your MySQL server
version for the right syntax to use near 'BIGSERIAL PRIMARY KEY NOT
NULL, access_token VARCHAR(500) NOT NULL, userid B' at line 2
scripts
CREATE DATABASE bank_account;
CREATE TABLE bank_user(
userid BIGSERIAL PRIMARY KEY NOT NULL,
first_name VARCHAR(32) NOT NULL,
last_name VARCHAR(32) NOT NULL,
email VARCHAR(32) NOT NULL,
password VARCHAR(255) NOT NULL,
unique(email)
);
CREATE TABLE TOKENS(
id BIGSERIAL PRIMARY KEY NOT NULL,
access_token VARCHAR(500) NOT NULL,
userid BIGSERIAL NOT NULL,
FOREIGN KEY(userid) REFERENCES bank_user(userid)
);
I get this error I tried with backticks as but unfortunately didn't work...I would appreciate your insight.
Try this:
CREATE TABLE bank_user( userid BIGINT PRIMARY KEY NOT NULL, first_name VARCHAR(32) NOT NULL, last_name VARCHAR(32) NOT NULL, email VARCHAR(32) NOT NULL, password VARCHAR(255) NOT NULL, unique(email) );
CREATE TABLE TOKENS( id BIGINT PRIMARY KEY NOT NULL, access_token VARCHAR(500) NOT NULL, userid BIGINT NOT NULL, FOREIGN KEY(userid) REFERENCES bank_user(userid) );
check the following link for mappings between PostgreSQL and MySQL.
In your case, instead of BIGSERIAL you need to use BIGINT.
The equivalent in MySQL would be:
CREATE TABLE bank_user (
userid BIGINT AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(32) NOT NULL,
last_name VARCHAR(32) NOT NULL,
email VARCHAR(32) NOT NULL,
password VARCHAR(255) NOT NULL,
unique(email)
);
CREATE TABLE TOKENS (
id BIGINT AUTO_INCREMENT PRIMARY KEY,
access_token VARCHAR(500) NOT NULL,
userid BIGINT NOT NULL,
FOREIGN KEY (userid) REFERENCES bank_user(userid)
);
Here is a db<>fiddle.
The equivalent of SERIAL is AUTO_INCREMENT.
Some comments:
32 characters may or may not be big enough for names. It is certainly not big enough for emails, which can get pretty long.
The password should be encrypted. Do not store clear-text passwords in the data.
I would be surprised if you really needed BIGINT for the userid. Are you really planning on having billions of users? A 4-byte integer should be sufficient.
NOT NULL is redundant when you declare a column to be the primary key.

Turn python sqlalchemy class into raw SQL (create table with foreign key)

I have a python app and I use sqlalchemy and posgreSQL. I have already some data in the DB and do not want to recreate it, so I want to add a table to the DB and keep all data (earlier I used alembic for that, but I want to use raw SQL this time).
This is my new table, which I want to add to the DB in python:
# User payed data
class UserPayData(Base):
__tablename__ = 'userspaydata'
id = Column(Integer, primary_key=True)
account_owner = Column(Text, nullable=False)
IBAN = Column(Text, nullable=False)
# Foreign Keys
belongs_to_user_id = Column(Integer, ForeignKey('users.id'))
payment_belongs_to_user_relation = relationship("User", back_populates="payment_belongs_to_user_addresses")
def __init__(self, account_owner=None, IBAN=None):
self.account_owner = account_owner
self.IBAN = IBAN
def get(self, id):
if self.id == id:
return self
else:
return None
def __repr__(self):
return '<%s(%r, %r, %r)>' % (self.__class__.__name__, self.id, self.account_owner, self.IBAN)
Here is the relevant part from the users table:
# Project
class User(UserMixin, Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
# etc...
# Foreign Key payment
payment_belongs_to_user_addresses = relationship('UserPayData', back_populates="payment_belongs_to_user_relation")
This is the raw SQL which I want to execute:
CREATE TABLE 'userspaydata'
(
account_owner TEXT NOT NULL,
IBAN TEXT NOT NULL,
belongs_to_user_id INT references users(id)
);
I have found many examples and some are different.
I have a few questions:
Do I have to create a primary key aswell? AFAIK, if the primary key is not defined by me it will be automatically ID INT
Relationships do not need to be defined on the DB level, they are defined on the app level or am I wrong? If I am wrong, how I define the relationship in raw SQL?
I have not submitted the SQL query yet, is the syntax correct?
Do I have to create a primary key aswell
Yes, if you want to work with the tables using sqlalchemy Declarative Mappings, and in general its a great idea to specify the primary key.
Relationships do not need to be defined on the DB level, they are defined on the app level or am I wrong?
Foreign Keys defined in your Sqlalchemy Declarative classes are mapped to database Foreign Key Constraints.
Again, relationships in relational databases are another great idea. Specify foreign key dependencies unless you have a specific reason not to.
If you don't specify constraints in the database you can end up with corrupted data.
If you are emitting DDL statements manually, you can specify the foreign key constraints. Foreign Keys in the Postgres Documentation
I have not submitted the SQL query yet, is the syntax correct?
No, Here's a corrected version of your DDL statement.
CREATE TABLE userspaydata (
id INTEGER PRIMARY KEY,
account_owner TEXT NOT NULL,
IBAN TEXT NOT NULL,
belongs_to_user_id INT references users(id) -- this is a FK constraint
);
Note that id doesn't auto-increment. For auto-increment id
change the id column definition to
-- postgrseql 10 & above
id INTEGER PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY
-- postgresql 9.6 & below
id SERIAL PRIMARY KEY
and change the sqlalchemy mapping for id column to
id = Column(Integer, primary_key=True, auto_increment=True)
This is my solution and it seems to work (I also added datetime):
CREATE TABLE userspaydata (
Id SERIAL primary key,
account_owner TEXT NOT NULL,
IBAN TEXT NOT NULL,
date_added TIMESTAMP WITH TIME ZONE,
belongs_to_user_id INT references users(id)
);
PK must be defined by me.
Relationships are handled on the app level.

Set AUTOINCREMENT value in django table

I have the following table in mysql:
CREATE TABLE `portal_asset` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`asset_id` int(11) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=1000000 DEFAULT CHARSET=utf8;
How would I create thie same table in django? So far I have the following, but not sure how to set the AUTO_INCREMENT value --
class PortalAsset(models.Model):
id = models.IntegerField(primary_key=True)
asset_id = models.IntegerField(unique=True)
class Meta:
db_table = u'portal_asset'
How can I set the AUTO_INCREMENT value to start off at 1000000 ? The equivalent of:
alter table portal_asset AUTO_INCREMENT=1000000;
You can use a RunSQL operation in your migrations to execute the necessary SQL:
migrations.RunSQL("ALTER TABLE portal_asset AUTO_INCREMENT=1000000;")
If you haven't run any migrations, you can add this to your first migration to ensure no rows are inserted before the new value is set. Otherwise you'll have to add this operation in a new migration. You can create a new, empty migration using python manage.py makemigrations --empty <yourappname>.

Retrieving ForeignKey mapped objects in Python with SqlAlchemy

I have an existing database that I'm trying to map into SqlAlchemy's ORM. I want it to just figure out the ForiegnKey relations that already exist in the database itself.
Here's the code I have so far:
from sqlalchemy import create_engine, MetaData, Table
from sqlalchemy.orm import create_session
from sqlalchemy.ext.declarative import declarative_base
# connect to database and infer from structure
Base = declarative_base()
engine = create_engine("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
metadata = MetaData(bind=engine)
class Club(Base):
__table__ = Table('clubs', metadata, autoload=True)
def __repr__(self):
return "<Club: %s>" % (self.name,)
class Member(Base):
__table__ = Table('members', metadata, autoload=True)
def __repr__(self):
return "<Member: %s of %d>" % (self.name, self.club_id)
Here's the SQL table dump:
CREATE TABLE `clubs` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(45) collate utf8_bin default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
CREATE TABLE `members` (
`id` int(11) NOT NULL auto_increment,
`club_id` int(11) default NULL,
`name` VARCHAR(100) default NULL,
PRIMARY KEY (`id`),
KEY `fk_members_club_idx` (`club_id`),
CONSTRAINT `fk_members_club` FOREIGN KEY (`club_id`) REFERENCES `clubs` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
My problem is that in the Member __repr__ function, for example, instead of printing the club_id (which is useless to human), I'd like to print the Club name. Something like member.club.name would be ideal.
I couldn't find out how to do this on the SqlAlchemy docs unless I defined my own tables, not merely reflecting them in my code, which is what I'm doing here.
Just change your Member class to look like below:
class Member(Base):
__table__ = Table('members', metadata, autoload=True)
club = relationship(Club, backref="members")
def __repr__(self):
return "<Member: %s of %s>" % (self.name, self.club.name)
The point being that the reflection (autoload) will not automatically create relationships between classes, so you have to define them explicitly.

Django - set ForeignKey deferrable foreign key constraint in SQLite3

I seem to be stuck with creating an initialy deferrable foreign key relationship between two models in Django and using SQLite3 as my backend storage.
Consider this simple example. This is what models.py looks like:
from django.db import models
class Investigator(models.Model):
name = models.CharField(max_length=250)
email = models.CharField(max_length=250)
class Project(models.Model):
name = models.CharField(max_length=250)
investigator = models.ForeignKey(Investigator)
And this is what the output from sqlall looks like:
BEGIN;
CREATE TABLE "moo_investigator" (
"id" integer NOT NULL PRIMARY KEY,
"name" varchar(250) NOT NULL,
"email" varchar(250) NOT NULL
)
;
CREATE TABLE "moo_project" (
"id" integer NOT NULL PRIMARY KEY,
"name" varchar(250) NOT NULL,
"investigator_id" integer NOT NULL REFERENCES "moo_investigator" ("id")
)
;
CREATE INDEX "moo_project_a7e50be7" ON "moo_project" ("investigator_id");
COMMIT;
"DEFERRABLE INITIALLY DEFERRED" is missing from the *investigator_id* column in the project table. What am I doing wrong?
p.s. I am new to Python and Django - using Python version 2.6.1 Django version 1.4 and SQLite version 3.6.12
This behavior is now the default. See https://github.com/django/django/blob/803840abf7dcb6ac190f021a971f1e3dc8f6792a/django/db/backends/sqlite3/schema.py#L16
Sqlite backend does not add "DEFERRABLE INITIALLY DEFERRED". Check the code

Categories